aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCooldowns.java
diff options
context:
space:
mode:
authorBuildTools <james.jenour@protonmail.com>2021-01-21 02:41:02 +0800
committerBuildTools <james.jenour@protonmail.com>2021-01-21 02:41:02 +0800
commitca13cc0c881480a8d3f0d653eab937f336fd870e (patch)
tree56ac6b5386bd7f42d39600ce90664e53fa8378bc /src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCooldowns.java
parent3255cfce951367c9303297205f64577ef1eac650 (diff)
downloadnotenoughupdates-ca13cc0c881480a8d3f0d653eab937f336fd870e.tar.gz
notenoughupdates-ca13cc0c881480a8d3f0d653eab937f336fd870e.tar.bz2
notenoughupdates-ca13cc0c881480a8d3f0d653eab937f336fd870e.zip
PRE8
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCooldowns.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCooldowns.java135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCooldowns.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCooldowns.java
new file mode 100644
index 00000000..8edd9217
--- /dev/null
+++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCooldowns.java
@@ -0,0 +1,135 @@
+package io.github.moulberry.notenoughupdates.miscfeatures;
+
+import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
+import net.minecraft.block.state.IBlockState;
+import net.minecraft.client.Minecraft;
+import net.minecraft.item.ItemStack;
+import net.minecraft.network.play.server.S23PacketBlockChange;
+import net.minecraft.util.BlockPos;
+import net.minecraftforge.client.event.ClientChatReceivedEvent;
+import net.minecraftforge.event.world.WorldEvent;
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
+import net.minecraftforge.fml.common.gameevent.TickEvent;
+
+import java.util.*;
+import java.util.regex.Pattern;
+
+public class ItemCooldowns {
+
+ private static Map<ItemStack, Float> durabilityOverrideMap = new HashMap<>();
+ private static long pickaxeUseCooldownMillisRemaining = -1;
+ private static long treecapitatorCooldownMillisRemaining = -1;
+ private static long lastMillis = 0;
+
+ public static TreeMap<Long, BlockPos> blocksClicked = new TreeMap<>();
+
+ @SubscribeEvent
+ public void tick(TickEvent.ClientTickEvent event) {
+ if(event.phase == TickEvent.Phase.END) {
+ long currentTime = System.currentTimeMillis();
+
+ Long key;
+ while((key = blocksClicked.floorKey(currentTime - 1500)) != null) {
+ blocksClicked.remove(key);
+ }
+
+ long millisDelta = currentTime - lastMillis;
+ lastMillis = currentTime;
+
+ durabilityOverrideMap.clear();
+
+ if(pickaxeUseCooldownMillisRemaining >= 0) {
+ pickaxeUseCooldownMillisRemaining -= millisDelta;
+ }
+ if(treecapitatorCooldownMillisRemaining >= 0) {
+ treecapitatorCooldownMillisRemaining -= millisDelta;
+ }
+ }
+ }
+
+ @SubscribeEvent
+ public void onWorldUnload(WorldEvent.Unload event) {
+ blocksClicked.clear();
+ }
+
+ public static void blockClicked(BlockPos pos) {
+ long currentTime = System.currentTimeMillis();
+ blocksClicked.put(currentTime, pos);
+ }
+
+ public static void processBlockChangePacket(S23PacketBlockChange packetIn) {
+ BlockPos pos = packetIn.getBlockPosition();
+
+ if(blocksClicked.containsValue(pos)) {
+ IBlockState oldState = Minecraft.getMinecraft().theWorld.getBlockState(pos);
+ if(oldState.getBlock() != packetIn.getBlockState().getBlock()) {
+ onBlockMined(pos);
+ }
+ }
+ }
+
+ public static void onBlockMined(BlockPos pos) {
+ ItemStack held = Minecraft.getMinecraft().thePlayer.getHeldItem();
+ String internalname = NotEnoughUpdates.INSTANCE.manager.getInternalNameForItem(held);
+ if(internalname != null) {
+ if(treecapitatorCooldownMillisRemaining < 0 &&
+ (internalname.equals("TREECAPITATOR_AXE") || internalname.equals("JUNGLE_AXE"))) {
+ treecapitatorCooldownMillisRemaining = 2*1000;
+ }
+ }
+ }
+
+ private static Pattern PICKAXE_ABILITY_REGEX = Pattern.compile("\\u00a7r\\u00a7aYou used your " +
+ "\\u00a7r\\u00a7e.+ \\u00a7r\\u00a7aPickaxe Ability!\\u00a7r");
+
+ @SubscribeEvent
+ public void onChatMessage(ClientChatReceivedEvent event) {
+ if(PICKAXE_ABILITY_REGEX.matcher(event.message.getFormattedText()).matches()) {
+ pickaxeUseCooldownMillisRemaining = 120*1000;
+ }
+ }
+
+ public static float getDurabilityOverride(ItemStack stack) {
+ if(Minecraft.getMinecraft().theWorld == null) return -1;
+
+ if(durabilityOverrideMap.containsKey(stack)) {
+ return durabilityOverrideMap.get(stack);
+ }
+
+ String internalname = NotEnoughUpdates.INSTANCE.manager.getInternalNameForItem(stack);
+ if(internalname == null) {
+ durabilityOverrideMap.put(stack, -1f);
+ return -1;
+ }
+
+ if(internalname.endsWith("_PICKAXE") || internalname.contains("_DRILL_")) {
+ if(pickaxeUseCooldownMillisRemaining < 0) {
+ durabilityOverrideMap.put(stack, -1f);
+ return -1;
+ }
+
+ if(pickaxeUseCooldownMillisRemaining > 120*1000) {
+ return stack.getItemDamage();
+ }
+ float dura = (float)(pickaxeUseCooldownMillisRemaining/(120.0*1000.0));
+ durabilityOverrideMap.put(stack, dura);
+ return dura;
+ } else if(internalname.equals("TREECAPITATOR_AXE") || internalname.equals("JUNGLE_AXE")) {
+ if(treecapitatorCooldownMillisRemaining < 0) {
+ durabilityOverrideMap.put(stack, -1f);
+ return -1;
+ }
+
+ if(treecapitatorCooldownMillisRemaining > 2*1000) {
+ return stack.getItemDamage();
+ }
+ float dura = (float)(treecapitatorCooldownMillisRemaining/(2.0*1000.0));
+ durabilityOverrideMap.put(stack, dura);
+ return dura;
+ }
+
+ durabilityOverrideMap.put(stack, -1f);
+ return -1;
+ }
+
+}