Skip to content

Commit

Permalink
implement Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilzinsel64 committed Feb 7, 2024
1 parent c232eb7 commit f8b5141
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 90 deletions.
67 changes: 54 additions & 13 deletions src/main/java/ganymedes01/etfuturum/backhand/Backhand.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package ganymedes01.etfuturum.backhand;

import ganymedes01.etfuturum.api.backhand.BackhandExtendedProperty;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;

public class Backhand {
public static Backhand INSTANCE = new Backhand();

public static boolean OffhandAttack = false;
public static boolean EmptyOffhand = false;
public static boolean OffhandBreakBlocks = false;
public static boolean UseOffhandArrows = true;
public static boolean UseOffhandBow = true;
public static boolean OffhandTickHotswap = true;
public static int AlternateOffhandSlot = 9;
public static boolean UseInventorySlot = false;
public static String[] offhandBlacklist;

public static boolean RenderEmptyOffhandAtRest = false;

public static boolean isOffhandBlacklisted(ItemStack stack) {
public boolean OffhandAttack = false;
public boolean EmptyOffhand = false;
public boolean OffhandBreakBlocks = false;
public boolean UseOffhandArrows = true;
public boolean UseOffhandBow = true;
public boolean OffhandTickHotswap = true;
public int AlternateOffhandSlot = 9;
public boolean UseInventorySlot = false;
public String[] offhandBlacklist;
public boolean RenderEmptyOffhandAtRest = false;

public boolean isOffhandBlacklisted(ItemStack stack) {
if (stack == null)
return false;

Expand All @@ -28,4 +32,41 @@ public static boolean isOffhandBlacklisted(ItemStack stack) {
}
return false;
}

public void setPlayerOffhandItem(EntityPlayer player, ItemStack stack) {
if (!isOffhandBlacklisted(stack)) {
if (UseInventorySlot) {
player.inventory.setInventorySlotContents(AlternateOffhandSlot, stack);
} else {
getOffhandEP(player).setOffhandItem(stack);
}
}
}

public ItemStack getOffhandItem(EntityPlayer player) {
if (UseInventorySlot) {
return player.inventory.getStackInSlot(AlternateOffhandSlot);
} else {
return getOffhandEP(player).getOffhandItem();
}
}

public void swapOffhandItem(EntityPlayer player) {
final ItemStack mainhandItem = player.getCurrentEquippedItem();
final ItemStack offhandItem = getOffhandItem(player);
setPlayerCurrentItem(player, offhandItem);
setPlayerOffhandItem(player, mainhandItem);
}

public static BackhandExtendedProperty getOffhandEP(EntityPlayer player) {
return ((BackhandExtendedProperty)player.getExtendedProperties("OffhandStorage"));
}

public static boolean hasOffhandInventory(EntityPlayer player) {
return player.inventory instanceof InventoryPlayerBattle;
}

public static void setPlayerCurrentItem(EntityPlayer player, ItemStack stack) {
player.inventory.setInventorySlotContents(player.inventory.currentItem, stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ public void clientHelper(TickEvent.PlayerTickEvent event) {
delay--;
}

if (!Backhand.OffhandBreakBlocks) {
if (!Backhand.INSTANCE.OffhandBreakBlocks) {
return;
}

if (!Backhand.EmptyOffhand && BattlegearUtils.getOffhandItem(event.player) == null) {
if (!Backhand.INSTANCE.EmptyOffhand && BattlegearUtils.getOffhandItem(event.player) == null) {
return;
}

Expand Down Expand Up @@ -292,7 +292,7 @@ public void resetPlayerLeftHand(RenderPlayerEvent.Post event){

@SubscribeEvent
public void render3rdPersonOffhand(RenderPlayerEvent.Specials.Post event) {
if (!Backhand.EmptyOffhand && BattlegearUtils.getOffhandItem(event.entityPlayer) == null) {
if (!Backhand.INSTANCE.EmptyOffhand && BattlegearUtils.getOffhandItem(event.entityPlayer) == null) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ganymedes01.etfuturum.backhand.client.renderer.RenderOffhandPlayer;
import ganymedes01.etfuturum.backhand.packets.BackhandSwapPacket;
import ganymedes01.etfuturum.backhand.packets.BackhandWorldHotswapPacket;
import ganymedes01.etfuturum.network.BackhandHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.gui.GuiIngame;
Expand Down Expand Up @@ -60,9 +61,9 @@ public void onPlayerInteractNonVanilla(PlayerInteractEvent event) {
if(event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR) {
EntityPlayer player = event.entityPlayer;
ItemStack mainhandItem = player.getHeldItem();
ItemStack offhandItem = BattlegearUtils.getOffhandItem(player);
ItemStack offhandItem = Backhand.INSTANCE.getOffhandItem(player);
if((mainhandItem == null || mainhandItem.getItem() != Items.fireworks) && offhandItem != null && offhandItem.getItem() == Items.fireworks) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
fireworkHotSwapped = 1;
}
}
Expand All @@ -74,10 +75,10 @@ public void onLivingDeath(LivingDeathEvent event) {
return;

EntityPlayer player = (EntityPlayer) event.entityLiving;
if (!BattlegearUtils.hasOffhandInventory(player)) {
ItemStack offhandItem = BattlegearUtils.getOffhandItem(player);
if (!Backhand.INSTANCE.hasOffhandInventory(player)) {
ItemStack offhandItem = Backhand.INSTANCE.getOffhandItem(player);
player.func_146097_a(offhandItem, true, false);
BattlegearUtils.setPlayerOffhandItem(player,null);
Backhand.INSTANCE.setPlayerOffhandItem(player,null);
}
}

Expand All @@ -89,14 +90,14 @@ public void onLivingHurt(LivingHurtEvent event) {
Class<?> totemItem = Class.forName("ganymedes01.etfuturum.items.ItemTotemUndying");

EntityPlayer player = (EntityPlayer) event.entityLiving;
ItemStack offhandItem = BattlegearUtils.getOffhandItem(player);
ItemStack offhandItem = Backhand.INSTANCE.getOffhandItem(player);
ItemStack mainhandItem = player.getCurrentEquippedItem();
if (offhandItem == null) {
return;
}

if (totemItem.isInstance(offhandItem.getItem()) && (mainhandItem == null || !totemItem.isInstance(mainhandItem.getItem()))) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
regularHotSwap = true;
MinecraftForge.EVENT_BUS.post(event);
}
Expand All @@ -106,11 +107,11 @@ public void onLivingHurt(LivingHurtEvent event) {
@SubscribeEvent
public void onItemUseStart(PlayerUseItemEvent.Start event) {
EntityPlayer player = event.entityPlayer;
ItemStack offhandItem = BattlegearUtils.getOffhandItem(player);
ItemStack offhandItem = Backhand.INSTANCE.getOffhandItem(player);
ItemStack mainhandItem = player.getCurrentEquippedItem();

//boolean offHandUse = BattlegearUtils.checkForRightClickFunction(offhandItem);
boolean mainhandUse = BattlegearUtils.checkForRightClickFunction(mainhandItem);
boolean mainhandUse = Backhand.INSTANCE.checkForRightClickFunction(mainhandItem);

if (offhandItem != null && !mainhandUse) {
event.setCanceled(true);
Expand All @@ -119,12 +120,12 @@ public void onItemUseStart(PlayerUseItemEvent.Start event) {

@SubscribeEvent
public void onArrowNock(ArrowNockEvent event) {
if (!Backhand.UseOffhandArrows) {
if (!Backhand.INSTANCE.UseOffhandArrows) {
return;
}

boolean overrideWithOffhand = false;
ItemStack offhandItem = BattlegearUtils.getOffhandItem(event.entityPlayer);
ItemStack offhandItem = Backhand.INSTANCE.getOffhandItem(event.entityPlayer);
if (offhandItem != null) {
try {
Class<?> etFuturumArrow = Class.forName("ganymedes01.etfuturum.items.ItemArrowTipped");
Expand All @@ -147,14 +148,14 @@ public void onArrowNock(ArrowNockEvent event) {
@SubscribeEvent
public void onItemFinish(PlayerUseItemEvent.Finish event) {
EntityPlayer player = event.entityPlayer;
ItemStack offhandItem = BattlegearUtils.getOffhandItem(player);
ItemStack offhandItem = Backhand.INSTANCE.getOffhandItem(player);
ItemStack mainhandItem = player.getCurrentEquippedItem();
boolean mainhandUse = BattlegearUtils.checkForRightClickFunction(mainhandItem);
boolean mainhandUse = Backhand.INSTANCE.checkForRightClickFunction(mainhandItem);
if (offhandItem == null || mainhandUse) {
return;
}
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && !ServerTickHandler.tickStartItems.containsKey(player.getUniqueID())) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
regularHotSwap = true;
}
}
Expand All @@ -163,22 +164,22 @@ public void onItemFinish(PlayerUseItemEvent.Finish event) {
public void onItemStop(PlayerUseItemEvent.Stop event) {
EntityPlayer player = event.entityPlayer;
ItemStack mainhandItem = player.getCurrentEquippedItem();
boolean mainhandUse = BattlegearUtils.checkForRightClickFunction(mainhandItem);
if (BattlegearUtils.getOffhandItem(player) == null || mainhandUse) {
boolean mainhandUse = Backhand.INSTANCE.checkForRightClickFunction(mainhandItem);
if (Backhand.INSTANCE.getOffhandItem(player) == null || mainhandUse) {
return;
}

if (!ServerTickHandler.tickStartItems.containsKey(player.getUniqueID()) && !regularHotSwap) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
regularHotSwap = true;
}

if (!Backhand.UseOffhandArrows || !(event.item.getItem() instanceof ItemBow)) {
if (!Backhand.INSTANCE.UseOffhandArrows || !(event.item.getItem() instanceof ItemBow)) {
return;
}

boolean overrideWithOffhand = false;
ItemStack offhandItem = BattlegearUtils.getOffhandItem(event.entityPlayer);
ItemStack offhandItem = Backhand.INSTANCE.getOffhandItem(event.entityPlayer);
if (offhandItem != null) {
try {
Class<?> etFuturumArrow = Class.forName("ganymedes01.etfuturum.items.ItemArrowTipped");
Expand All @@ -194,7 +195,7 @@ public void onItemStop(PlayerUseItemEvent.Stop event) {
if (overrideWithOffhand) {
arrowHotSwapped = true;
if (offhandItem.getItem() != Items.arrow) {
BattlegearUtils.swapOffhandItem(event.entityPlayer);
Backhand.INSTANCE.swapOffhandItem(event.entityPlayer);
}
}
}
Expand All @@ -207,19 +208,19 @@ public void onUpdateWorld(TickEvent.WorldTickEvent event) {
return;
}

if (Backhand.OffhandTickHotswap) {
if (Backhand.INSTANCE.OffhandTickHotswap) {
List<EntityPlayer> players = event.world.playerEntities;
for (EntityPlayer player : players) {
ItemStack mainhand = player.getCurrentEquippedItem() == null ? null : player.getCurrentEquippedItem().copy();
ItemStack offhand = BattlegearUtils.getOffhandItem(player) == null ? null : BattlegearUtils.getOffhandItem(player).copy();
ItemStack offhand = Backhand.INSTANCE.getOffhandItem(player) == null ? null : Backhand.INSTANCE.getOffhandItem(player).copy();
if (offhand == null) {
continue;
}

if (event.phase == TickEvent.Phase.START && !player.isUsingItem()) {
if (!BattlegearUtils.checkForRightClickFunction(mainhand)) {
if (!tickStartItems.containsKey(player.getUniqueID())) {
Backhand.packetHandler.sendPacketToPlayer(
Backhand.INSTANCE.packetHandler.sendPacketToPlayer(
new BackhandWorldHotswapPacket(true).generatePacket(), (EntityPlayerMP) player
);
}
Expand All @@ -236,9 +237,9 @@ public void onUpdateWorld(TickEvent.WorldTickEvent event) {
public static void resetTickingHotswap(EntityPlayer player) {
if (tickStartItems.containsKey(player.getUniqueID())) {
player.setCurrentItemOrArmor(0, tickStartItems.get(player.getUniqueID()).get(0));
BattlegearUtils.setPlayerOffhandItem(player, tickStartItems.get(player.getUniqueID()).get(1));
Backhand.INSTANCE.setPlayerOffhandItem(player, tickStartItems.get(player.getUniqueID()).get(1));
tickStartItems.remove(player.getUniqueID());
Backhand.packetHandler.sendPacketToPlayer(
BackhandHandler.packetHandler.sendPacketToPlayer(
new BackhandWorldHotswapPacket(false).generatePacket(), (EntityPlayerMP) player
);
}
Expand All @@ -252,27 +253,27 @@ public void onUpdatePlayer(TickEvent.PlayerTickEvent event)
EntityPlayer player = event.player;
if (FMLCommonHandler.instance().getEffectiveSide() != Side.SERVER) {
if (ServerEventsHandler.regularHotSwap) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
ServerEventsHandler.regularHotSwap = false;
}
return;
}

ItemStack offhand = BattlegearUtils.getOffhandItem(player);
ItemStack offhand = Backhand.INSTANCE.getOffhandItem(player);

if (event.phase == TickEvent.Phase.END) {
if (blacklistDelay > 0) {
blacklistDelay--;
}
if (Backhand.isOffhandBlacklisted(offhand)) {
if (Backhand.INSTANCE.isOffhandBlacklisted(offhand)) {
if (!ItemStack.areItemStacksEqual(offhand,prevStackInSlot)) {
blacklistDelay = 10;
} else if (blacklistDelay == 0) {
BattlegearUtils.setPlayerOffhandItem(player,null);
Backhand.INSTANCE.setPlayerOffhandItem(player,null);

boolean foundSlot = false;
for (int i = 0; i < player.inventory.getSizeInventory() - 4; i++) {
if (i == Backhand.AlternateOffhandSlot)
if (i == Backhand.INSTANCE.AlternateOffhandSlot)
continue;
if (player.inventory.getStackInSlot(i) == null) {
player.inventory.setInventorySlotContents(i,offhand);
Expand All @@ -289,32 +290,32 @@ public void onUpdatePlayer(TickEvent.PlayerTickEvent event)
prevStackInSlot = offhand;
}

if (BattlegearUtils.getOffhandEP(player).syncOffhand) {
if (Backhand.INSTANCE.getOffhandEP(player).syncOffhand) {
if (!tickStartItems.containsKey(player.getUniqueID())) {
Backhand.packetHandler.sendPacketToAll(new BattlegearSyncItemPacket(player).generatePacket());
BackhandHandler.packetHandler.sendPacketToAll(new BattlegearSyncItemPacket(player).generatePacket());
}
BattlegearUtils.getOffhandEP(player).syncOffhand = false;
Backhand.INSTANCE.getOffhandEP(player).syncOffhand = false;
}

if (arrowHotSwapped) {
if (offhand != null && offhand.getItem() != Items.arrow) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
}
arrowHotSwapped = false;
}
if (regularHotSwap) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
regularHotSwap = false;
}

if (fireworkHotSwapped > 0) {
fireworkHotSwapped--;
} else if (fireworkHotSwapped == 0) {
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
fireworkHotSwapped--;
MinecraftForge.EVENT_BUS.post(new PlayerInteractEvent(player, PlayerInteractEvent.Action.RIGHT_CLICK_AIR,
(int)player.posX, (int)player.posY, (int)player.posZ, -1, player.worldObj));
BattlegearUtils.swapOffhandItem(player);
Backhand.INSTANCE.swapOffhandItem(player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public BackhandConfigGui(GuiScreen parent) {
@Override
public void initGui() {
this.buttonList.add(new GuiButton(1, this.width / 2 - 75, this.height - 38, I18n.format("gui.done")));
this.buttonList.add(new GuiToggleButton(10, this.width / 2 - 75, this.height / 2 - 12, I18n.format("backhandconfig.offhandRest") + ":" + Backhand.RenderEmptyOffhandAtRest, this.fontRendererObj));
this.buttonList.add(new GuiToggleButton(10, this.width / 2 - 75, this.height / 2 - 12, I18n.format("backhandconfig.offhandRest") + ":" + Backhand.INSTANCE.RenderEmptyOffhandAtRest, this.fontRendererObj));

for (Object obj : this.buttonList) {
((GuiButton)obj).xPosition = this.width/2 - ((GuiButton)obj).getButtonWidth()/2;
Expand All @@ -33,7 +33,7 @@ protected void actionPerformed(GuiButton button) {
FMLClientHandler.instance().showGuiScreen(parent);
}
if (button.id == 10) {
Backhand.RenderEmptyOffhandAtRest = !Backhand.RenderEmptyOffhandAtRest;
Backhand.INSTANCE.RenderEmptyOffhandAtRest = !Backhand.INSTANCE.RenderEmptyOffhandAtRest;
}
if(button instanceof GuiToggleButton){
((GuiToggleButton) button).toggleDisplayString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void write(ByteBuf out) {

@Override
public void process(ByteBuf inputStream, EntityPlayer sender) {
if (!ganymedes01.etfuturum.backhand.Backhand.OffhandAttack) {
if (!ganymedes01.etfuturum.Backhand.INSTANCE.Backhand.INSTANCE.OffhandAttack) {
return;
}

Expand Down
Loading

0 comments on commit f8b5141

Please sign in to comment.