diff options
author | bartimaeusnek <33183715+bartimaeusnek@users.noreply.github.com> | 2019-04-28 22:13:13 +0200 |
---|---|---|
committer | bartimaeusnek <33183715+bartimaeusnek@users.noreply.github.com> | 2019-04-28 22:13:13 +0200 |
commit | 5f41aa8bd449c8c63fd899c6640468fe9d86dc9f (patch) | |
tree | 2462ec079f2d29be1095392fc1b7c6e94375cf05 | |
parent | 7f895c5e164e8b73d953756a3c02b2aea2d405b6 (diff) | |
download | GT5-Unofficial-5f41aa8bd449c8c63fd899c6640468fe9d86dc9f.tar.gz GT5-Unofficial-5f41aa8bd449c8c63fd899c6640468fe9d86dc9f.tar.bz2 GT5-Unofficial-5f41aa8bd449c8c63fd899c6640468fe9d86dc9f.zip |
Tooltip Server Synchronisation
Signed-off-by: bartimaeusnek <33183715+bartimaeusnek@users.noreply.github.com>
Former-commit-id: 1ce6237155c71ee8329f6302c82b357cc6432214
6 files changed, 141 insertions, 9 deletions
diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/MainMod.java b/src/main/java/com/github/bartimaeusnek/bartworks/MainMod.java index 43b76254d2..da330424ee 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/MainMod.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/MainMod.java @@ -35,6 +35,7 @@ import com.github.bartimaeusnek.bartworks.common.loaders.BioLabLoader; import com.github.bartimaeusnek.bartworks.common.loaders.GTNHBlocks; import com.github.bartimaeusnek.bartworks.common.loaders.LoaderRegistry; import com.github.bartimaeusnek.bartworks.common.net.BW_Network; +import com.github.bartimaeusnek.bartworks.server.EventHandler.ServerEventHandler; import com.github.bartimaeusnek.bartworks.system.log.DebugLog; import com.github.bartimaeusnek.bartworks.system.material.ThreadedLoader; import com.github.bartimaeusnek.bartworks.system.material.Werkstoff; @@ -50,8 +51,6 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.event.FMLServerStartedEvent; import cpw.mods.fml.common.network.IGuiHandler; import cpw.mods.fml.common.network.NetworkRegistry; -import cpw.mods.fml.common.registry.GameData; -import cpw.mods.fml.common.registry.GameRegistry; import gregtech.api.enums.ItemList; import gregtech.api.enums.Materials; import gregtech.api.enums.SubTag; @@ -60,7 +59,6 @@ import gregtech.api.util.GT_Recipe; import gregtech.api.util.GT_Utility; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.init.Blocks; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraftforge.common.MinecraftForge; import org.apache.logging.log4j.LogManager; @@ -68,7 +66,6 @@ import org.apache.logging.log4j.Logger; import java.io.IOException; import java.util.HashSet; -import java.util.Iterator; import static com.github.bartimaeusnek.bartworks.common.tileentities.multis.GT_TileEntity_ElectricImplosionCompressor.eicMap; @@ -133,6 +130,8 @@ public final class MainMod { public void init(FMLInitializationEvent init) { if (FMLCommonHandler.instance().getSide().isClient() && ConfigHandler.BioLab) MinecraftForge.EVENT_BUS.register(new ClientEventHandler()); + if (FMLCommonHandler.instance().getSide().isServer()) + MinecraftForge.EVENT_BUS.register(new ServerEventHandler()); new LoaderRegistry().run(); if (ConfigHandler.BioLab) new BioLabLoader().run(); @@ -158,7 +157,6 @@ public final class MainMod { WerkstoffLoader.INSTANCE.run(); } } - @Mod.EventHandler public void onServerStarted(FMLServerStartedEvent event) { OreDictHandler.adaptCacheForWorld(); diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/client/ClientEventHandler/ClientEventHandler.java b/src/main/java/com/github/bartimaeusnek/bartworks/client/ClientEventHandler/ClientEventHandler.java index 861b456fd9..f4c155d246 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/client/ClientEventHandler/ClientEventHandler.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/client/ClientEventHandler/ClientEventHandler.java @@ -64,8 +64,8 @@ public class ClientEventHandler { ItemStack tmp = event.itemStack.copy(); Pair<Integer,Short> abstractedStack = new Pair<>(Item.getIdFromItem(tmp.getItem()), (short) tmp.getItemDamage()); List<String> tooAdd = new ArrayList<>(); - if (OreDictHandler.getCache().containsValue(abstractedStack)) { - for (Pair<Integer,Short> pair : OreDictHandler.getCache().values()) { + if (OreDictHandler.getNonBWCache().contains(abstractedStack)) { + for (Pair<Integer,Short> pair : OreDictHandler.getNonBWCache()) { if (pair.equals(abstractedStack)) { GameRegistry.UniqueIdentifier UI = GameRegistry.findUniqueIdentifierFor(tmp.getItem()); if (UI == null) diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/common/net/BW_Network.java b/src/main/java/com/github/bartimaeusnek/bartworks/common/net/BW_Network.java index cca0c39712..277823b5f4 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/common/net/BW_Network.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/common/net/BW_Network.java @@ -59,7 +59,7 @@ public class BW_Network extends MessageToMessageCodec<FMLProxyPacket, GT_Packet> public BW_Network() { this.mChannel = NetworkRegistry.INSTANCE.newChannel("BartWorks", new ChannelHandler[]{this, new HandlerShared()}); - this.mSubChannels = new GT_Packet[]{new RendererPacket(), new CircuitProgrammerPacket(), new OrePacket()}; + this.mSubChannels = new GT_Packet[]{new RendererPacket(), new CircuitProgrammerPacket(), new OrePacket(), new OreDictCachePacket()}; } protected void encode(ChannelHandlerContext aContext, GT_Packet aPacket, List<Object> aOutput) throws Exception { diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/common/net/OreDictCachePacket.java b/src/main/java/com/github/bartimaeusnek/bartworks/common/net/OreDictCachePacket.java new file mode 100644 index 0000000000..d44fc42cce --- /dev/null +++ b/src/main/java/com/github/bartimaeusnek/bartworks/common/net/OreDictCachePacket.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019 bartimaeusnek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.bartimaeusnek.bartworks.common.net; + +import com.github.bartimaeusnek.bartworks.system.oredict.OreDictHandler; +import com.github.bartimaeusnek.bartworks.util.Pair; +import com.google.common.io.ByteArrayDataInput; +import gregtech.api.net.GT_Packet; +import net.minecraft.world.IBlockAccess; + +import java.nio.ByteBuffer; +import java.util.HashSet; + +public class OreDictCachePacket extends GT_Packet { + + + private HashSet<Pair<Integer,Short>> hashSet = new HashSet<>(); + + public OreDictCachePacket() { + super(true); + } + + public OreDictCachePacket(HashSet<Pair<Integer,Short>> set) { + super(false); + hashSet = set; + } + + @Override + public byte getPacketID() { + return 3; + } + + @Override + public byte[] encode() { + int size = this.hashSet.size(); + ByteBuffer buff = ByteBuffer.allocate(4+size*4+size*2).putInt(size); + for(Pair<Integer,Short> p : this.hashSet) + buff.putInt(p.getKey()).putShort(p.getValue()); + return buff.array(); + } + + @Override + public GT_Packet decode(ByteArrayDataInput byteArrayDataInput) { + int size = byteArrayDataInput.readInt(); + for (int i = 0; i < size; i++) { + this.hashSet.add(new Pair<Integer,Short>(byteArrayDataInput.readInt(),byteArrayDataInput.readShort())); + } + return new OreDictCachePacket(this.hashSet); + } + + @Override + public void process(IBlockAccess iBlockAccess) { + OreDictHandler.getNonBWCache().clear(); + OreDictHandler.getNonBWCache().addAll(this.hashSet); + } +}
\ No newline at end of file diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/server/EventHandler/ServerEventHandler.java b/src/main/java/com/github/bartimaeusnek/bartworks/server/EventHandler/ServerEventHandler.java new file mode 100644 index 0000000000..9fc72b31cd --- /dev/null +++ b/src/main/java/com/github/bartimaeusnek/bartworks/server/EventHandler/ServerEventHandler.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 bartimaeusnek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.bartimaeusnek.bartworks.server.EventHandler; + +import com.github.bartimaeusnek.bartworks.MainMod; +import com.github.bartimaeusnek.bartworks.common.net.OreDictCachePacket; +import com.github.bartimaeusnek.bartworks.system.oredict.OreDictHandler; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.eventhandler.EventPriority; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraftforge.event.entity.EntityJoinWorldEvent; + +public class ServerEventHandler { + @SubscribeEvent(priority = EventPriority.LOWEST) + public void getTooltip(EntityJoinWorldEvent event) { + if (event == null || !(event.entity instanceof EntityPlayerMP) || !FMLCommonHandler.instance().getSide().isServer()) + return; + MainMod.BW_Network_instance.sendToPlayer(new OreDictCachePacket(OreDictHandler.getNonBWCache()), (EntityPlayerMP) event.entity); + } +} diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/system/oredict/OreDictHandler.java b/src/main/java/com/github/bartimaeusnek/bartworks/system/oredict/OreDictHandler.java index d70bf884d2..fb13cc7c85 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/system/oredict/OreDictHandler.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/system/oredict/OreDictHandler.java @@ -22,8 +22,12 @@ package com.github.bartimaeusnek.bartworks.system.oredict; +import com.github.bartimaeusnek.bartworks.MainMod; import com.github.bartimaeusnek.bartworks.util.Pair; +import com.github.bartimaeusnek.crossmod.BartWorksCrossmod; +import cpw.mods.fml.common.registry.GameRegistry; import gregtech.api.enums.OrePrefixes; +import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraftforge.oredict.OreDictionary; @@ -35,18 +39,31 @@ import java.util.Set; public class OreDictHandler { private static final HashMap<String, Pair<Integer,Short>> cache = new HashMap<>(); + private static final HashSet<Pair<Integer,Short>> cacheNonBW = new HashSet<>(); public static HashMap<String, Pair<Integer,Short>> getCache() { return OreDictHandler.cache; } + public static HashSet<Pair<Integer,Short>> getNonBWCache(){ + return OreDictHandler.cacheNonBW; + } + public static void adaptCacheForWorld(){ Set<String> used = new HashSet<>(cache.keySet()); OreDictHandler.cache.clear(); + OreDictHandler.cacheNonBW.clear(); for (String s : used) { if (!OreDictionary.getOres(s).isEmpty()) { ItemStack tmp = OreDictionary.getOres(s).get(0).copy(); - cache.put(s, new Pair<>(Item.getIdFromItem(tmp.getItem()), (short) tmp.getItemDamage())); + Pair<Integer,Short> p = new Pair<>(Item.getIdFromItem(tmp.getItem()), (short) tmp.getItemDamage()); + cache.put(s, p); + GameRegistry.UniqueIdentifier UI = GameRegistry.findUniqueIdentifierFor(tmp.getItem()); + if (UI == null) + UI = GameRegistry.findUniqueIdentifierFor(Block.getBlockFromItem(tmp.getItem())); + if (!UI.modId.equals(MainMod.MOD_ID) && !UI.modId.equals(BartWorksCrossmod.MOD_ID) && !UI.modId.equals("BWCore")){ + OreDictHandler.cacheNonBW.add(p); + } } } } |