-
Notifications
You must be signed in to change notification settings - Fork 755
/
ZoomModifier.java
107 lines (95 loc) · 4.54 KB
/
ZoomModifier.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package slimeknights.tconstruct.tools.modifiers.ability.armor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.UseAnim;
import slimeknights.mantle.client.TooltipKey;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.TinkerHooks;
import slimeknights.tconstruct.library.modifiers.hook.KeybindInteractModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.GeneralInteractionModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.InteractionSource;
import slimeknights.tconstruct.library.modifiers.impl.NoLevelsModifier;
import slimeknights.tconstruct.library.modifiers.util.ModifierHookMap.Builder;
import slimeknights.tconstruct.library.tools.capability.TinkerDataCapability;
import slimeknights.tconstruct.library.tools.capability.TinkerDataKeys;
import slimeknights.tconstruct.library.tools.context.EquipmentChangeContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
public class ZoomModifier extends NoLevelsModifier implements KeybindInteractModifierHook, GeneralInteractionModifierHook {
private static final ResourceLocation ZOOM = TConstruct.getResource("zoom");
@Override
public int getPriority() {
return 50;
}
@Override
public void onUnequip(IToolStackView tool, int level, EquipmentChangeContext context) {
if (context.getEntity().level.isClientSide) {
context.getTinkerData().ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.FOV_MODIFIER).remove(ZOOM));
}
}
@Override
public boolean startInteract(IToolStackView tool, ModifierEntry modifier, Player player, EquipmentSlot slot, TooltipKey keyModifier) {
if (player.isShiftKeyDown()) {
player.playSound(SoundEvents.SPYGLASS_USE, 1.0F, 1.0F);
if (player.level.isClientSide()) {
player.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.FOV_MODIFIER).set(ZOOM, 0.1f));
}
return true;
}
return false;
}
@Override
public void stopInteract(IToolStackView tool, ModifierEntry modifier, Player player, EquipmentSlot slot) {
player.playSound(SoundEvents.SPYGLASS_STOP_USING, 1.0F, 1.0F);
if (player.level.isClientSide()) {
player.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.FOV_MODIFIER).remove(ZOOM));
}
}
@Override
public InteractionResult onToolUse(IToolStackView tool, ModifierEntry modifier, Player player, InteractionHand hand, InteractionSource source) {
if (source == InteractionSource.RIGHT_CLICK) {
player.playSound(SoundEvents.SPYGLASS_USE, 1.0F, 1.0F);
player.startUsingItem(hand);
if (player.level.isClientSide) {
player.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.FOV_MODIFIER).set(ZOOM, 0.1f));
}
tool.getPersistentData().putBoolean(ZOOM, true);
return InteractionResult.CONSUME;
}
return InteractionResult.PASS;
}
@Override
public UseAnim getUseAction(IToolStackView tool, ModifierEntry modifier) {
return tool.getPersistentData().getBoolean(ZOOM) ? UseAnim.SPYGLASS : UseAnim.NONE;
}
@Override
public int getUseDuration(IToolStackView tool, ModifierEntry modifier) {
return tool.getPersistentData().getBoolean(ZOOM) ? 1200 : 0;
}
@Override
public boolean onFinishUsing(IToolStackView tool, ModifierEntry modifier, LivingEntity entity) {
if (tool.getPersistentData().getBoolean(ZOOM)) {
entity.playSound(SoundEvents.SPYGLASS_STOP_USING, 1.0F, 1.0F);
if (entity.level.isClientSide) {
entity.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.FOV_MODIFIER).remove(ZOOM));
}
tool.getPersistentData().remove(ZOOM);
return true;
}
return false;
}
@Override
public boolean onStoppedUsing(IToolStackView tool, ModifierEntry modifier, LivingEntity entity, int timeLeft) {
return onFinishUsing(tool, modifier, entity);
}
@Override
protected void registerHooks(Builder hookBuilder) {
super.registerHooks(hookBuilder);
hookBuilder.addHook(this, TinkerHooks.ARMOR_INTERACT, TinkerHooks.GENERAL_INTERACT);
}
}