Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reach ring rewrite #67

Merged
merged 6 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package vazkii.botania.common.core.handler;

import net.minecraft.client.Minecraft;
import vazkii.botania.common.item.equipment.bauble.ItemBauble;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class ItemBaubleHandler {

@SubscribeEvent
public void disconnectFromServer(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
if(Minecraft.getMinecraft().thePlayer != null) {
ItemBauble.removePlayer(Minecraft.getMinecraft().thePlayer.getUniqueID());
}
}

@SubscribeEvent
public void playerDisconnect(PlayerEvent.PlayerLoggedOutEvent event) {
ItemBauble.removePlayer(event.player.getUniqueID());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import vazkii.botania.common.core.handler.CommonTickHandler;
import vazkii.botania.common.core.handler.ConfigHandler;
import vazkii.botania.common.core.handler.InternalMethodHandler;
import vazkii.botania.common.core.handler.ItemBaubleHandler;
import vazkii.botania.common.core.handler.ManaNetworkHandler;
import vazkii.botania.common.core.handler.PixieHandler;
import vazkii.botania.common.core.handler.SheddingHandler;
Expand Down Expand Up @@ -145,6 +146,7 @@ public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(new SkyblockWorldEvents());

FMLCommonHandler.instance().bus().register(new CommonTickHandler());
FMLCommonHandler.instance().bus().register(new ItemBaubleHandler());

FMLInterModComms.sendMessage("ProjectE", "interdictionblacklist", EntityManaBurst.class.getCanonicalName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package vazkii.botania.common.item.equipment.bauble;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -38,13 +40,15 @@
@Optional.Interface(modid = "Thaumcraft", iface = "thaumcraft.api.IRunicArmor")
public abstract class ItemBauble extends ItemMod implements IBauble, ICosmeticAttachable, IPhantomInkable, IRunicArmor {

private static final String TAG_HASHCODE = "playerHashcode";
private static final String TAG_HASHCODE_CLIENT = "playerHashcodeClient";
private static final String TAG_BAUBLE_UUID_MOST = "baubleUUIDMost";
private static final String TAG_BAUBLE_UUID_LEAST = "baubleUUIDLeast";
private static final String TAG_COSMETIC_ITEM = "cosmeticItem";
private static final String TAG_PHANTOM_INK = "phantomInk";

private static final HashMap<UUID, UUID> itemToPlayerRemote = new HashMap<>();
private static final HashMap<UUID, UUID> itemToPlayer = new HashMap<>();
private static final HashSet<UUID> toRemoveItems = new HashSet<>();

public ItemBauble(String name) {
super();
setMaxStackSize(1);
Expand Down Expand Up @@ -123,9 +127,17 @@ public boolean canUnequip(ItemStack stack, EntityLivingBase player) {

@Override
public void onWornTick(ItemStack stack, EntityLivingBase player) {
if(getLastPlayerHashcode(stack, player.worldObj.isRemote) != player.hashCode()) {
UUID itemUUID = getBaubleUUID(stack);
if(toRemoveItems.contains(itemUUID)) {
// this is done like this because on server worn tick gets called after unequip
// so it would get reapplied
unapplyItem(itemUUID, player.worldObj.isRemote);
toRemoveItems.remove(itemUUID);
return;
}
if(!wasPlayerApplied(itemUUID, player.getUniqueID(), player.worldObj.isRemote)) {
onEquippedOrLoadedIntoWorld(stack, player);
setLastPlayerHashcode(stack, player.hashCode(), player.worldObj.isRemote);
applyToPlayer(itemUUID, player.getUniqueID(), player.worldObj.isRemote);
}
}

Expand All @@ -139,7 +151,7 @@ public void onEquipped(ItemStack stack, EntityLivingBase player) {
((EntityPlayer) player).addStat(ModAchievements.baubleWear, 1);

onEquippedOrLoadedIntoWorld(stack, player);
setLastPlayerHashcode(stack, player.hashCode(), player.worldObj.isRemote);
applyToPlayer(getBaubleUUID(stack), player.getUniqueID(), player.worldObj.isRemote);
}
}

Expand All @@ -149,7 +161,7 @@ public void onEquippedOrLoadedIntoWorld(ItemStack stack, EntityLivingBase player

@Override
public void onUnequipped(ItemStack stack, EntityLivingBase player) {
// NO-OP
toRemoveItems.add(getBaubleUUID(stack));
}

@Override
Expand Down Expand Up @@ -196,12 +208,37 @@ public static UUID getBaubleUUID(ItemStack stack) {
return new UUID(most, least);
}

public static int getLastPlayerHashcode(ItemStack stack, boolean remote) {
return ItemNBTHelper.getInt(stack, remote ? TAG_HASHCODE_CLIENT : TAG_HASHCODE, 0);
public static boolean wasPlayerApplied(UUID itemUUID, UUID playerUUID, boolean remote) {
return playerUUID.equals(remote ? itemToPlayerRemote.get(itemUUID) : itemToPlayer.get(itemUUID));
}

public static void setLastPlayerHashcode(ItemStack stack, int hash, boolean remote) {
ItemNBTHelper.setInt(stack, remote ? TAG_HASHCODE_CLIENT : TAG_HASHCODE, hash);
public static void applyToPlayer(UUID itemUUID, UUID playerUUID, boolean remote) {
if(remote) {
itemToPlayerRemote.put(itemUUID, playerUUID);
} else {
itemToPlayer.put(itemUUID, playerUUID);
}
}

public static void unapplyItem(UUID itemUUID, boolean remote) {
if(remote) {
itemToPlayerRemote.remove(itemUUID);
} else {
itemToPlayer.remove(itemUUID);
}
}

public static void removePlayer(UUID playerUUID) {
for(UUID item : itemToPlayerRemote.keySet().toArray(new UUID[0])) {
if(playerUUID.equals(itemToPlayerRemote.get(item))) {
itemToPlayerRemote.remove(item);
}
}
for(UUID item : itemToPlayer.keySet().toArray(new UUID[0])) {
if(playerUUID.equals(itemToPlayer.get(item))) {
itemToPlayer.remove(item);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.util.List;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.Tessellator;
Expand Down Expand Up @@ -53,6 +55,7 @@ public ItemBaubleCosmetic() {
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
icons = new IIcon[SUBTYPES];
for(int i = 0; i < SUBTYPES; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.awt.Color;
import java.util.List;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.OpenGlHelper;
Expand Down Expand Up @@ -68,6 +70,7 @@ public void getSubItems(Item item, CreativeTabs tab, List list) {
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
icons = new IIcon[4];
for(int i = 0; i < 4; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.List;
import java.util.Map;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
Expand Down Expand Up @@ -53,6 +55,7 @@ public BaubleType getBaubleType(ItemStack itemstack) {
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
super.registerIcons(par1IconRegister);
gemIcon = IconHelper.forItem(par1IconRegister, this, "Gem");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public ItemItemFinder() {
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
super.registerIcons(par1IconRegister);
gemIcon = IconHelper.forItem(par1IconRegister, this, "Gem");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package vazkii.botania.common.item.equipment.bauble;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.Tessellator;
Expand Down Expand Up @@ -47,6 +49,7 @@ public BaubleType getBaubleType(ItemStack itemstack) {
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
super.registerIcons(par1IconRegister);
gemIcon = IconHelper.forItem(par1IconRegister, this, "Gem");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void onEquippedOrLoadedIntoWorld(ItemStack stack, EntityLivingBase player
@Override
public void onUnequipped(ItemStack stack, EntityLivingBase player) {
Botania.proxy.setExtraReach(player, -3.5F);
super.onUnequipped(stack, player);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package vazkii.botania.common.item.equipment.bauble;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.Tessellator;
Expand Down Expand Up @@ -58,6 +60,7 @@ public BaubleType getBaubleType(ItemStack itemstack) {
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
super.registerIcons(par1IconRegister);
gemIcon = IconHelper.forItem(par1IconRegister, this, "Gem");
Expand Down