-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAbilityToggleItem.java
66 lines (62 loc) · 2.95 KB
/
AbilityToggleItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* PlayerAbilityLib
* Copyright (C) 2019-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package io.github.ladysnake.paltest;
import io.github.ladysnake.pal.AbilitySource;
import io.github.ladysnake.pal.Pal;
import io.github.ladysnake.pal.PlayerAbility;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
/**
* An item that toggles an arbitrary ability using PAL
*/
public class AbilityToggleItem extends Item {
private final PlayerAbility ability;
private final AbilitySource abilitySource;
public AbilityToggleItem(Settings settings, PlayerAbility abilityId, Identifier abilitySourceId) {
super(settings);
this.ability = abilityId;
this.abilitySource = Pal.getAbilitySource(abilitySourceId);
}
@Override
public ActionResult use(World world, PlayerEntity user, Hand hand) {
if (user instanceof ServerPlayerEntity sp) {
if (abilitySource.grants(sp, this.ability)) { // check whether the source is granting the ability
abilitySource.revokeFrom(sp, this.ability); // if it is, revoke it
} else {
abilitySource.grantTo(sp, this.ability); // otherwise, grant it
}
// Feedback message
user.sendMessage(Text.literal("")
.append(Text.literal(abilitySource.getId().toString()).styled(s -> s.withColor(Formatting.YELLOW)))
.append(abilitySource.grants(sp, this.ability) ? Text.literal(" added").styled(s -> s.withColor(Formatting.GREEN)) : Text.literal(" removed").styled(s -> s.withColor(Formatting.RED)))
.append(" (")
.append(Text.literal(this.ability.getId().toString()).styled(s -> s.withColor(Formatting.YELLOW)))
.append(" is ")
.append(ability.isEnabledFor(user) ? Text.literal("enabled").styled(s -> s.withColor(Formatting.GREEN)) : Text.literal("disabled").styled(s -> s.withColor(Formatting.RED)))
.append(")"), false);
}
return ActionResult.SUCCESS;
}
}