From ebfc4c6da63b8f9edbe49a4a873b9b7e1648312e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Tue, 28 Jun 2016 16:20:54 +0200 Subject: [PATCH] Network stuff moved to separate class --- src/java/mmm/food/FMilkable.java | 8 +-- src/java/mmm/tech/base/TechBase.java | 4 +- .../tech/base/alloy_furnace/AlloyFurnace.java | 3 +- .../base/alloy_furnace/TBAFContainer.java | 6 +- .../mmm/tech/base/alloy_furnace/TBAFGui.java | 6 +- src/java/mmm/utils/UNetwork.java | 61 +++++++++++++++++++ src/java/mmm/utils/URegistry.java | 33 ---------- 7 files changed, 75 insertions(+), 46 deletions(-) create mode 100644 src/java/mmm/utils/UNetwork.java diff --git a/src/java/mmm/food/FMilkable.java b/src/java/mmm/food/FMilkable.java index 492e6fa..2470894 100644 --- a/src/java/mmm/food/FMilkable.java +++ b/src/java/mmm/food/FMilkable.java @@ -6,7 +6,7 @@ import java.util.Set; import io.netty.buffer.ByteBuf; import mmm.Mmm; import mmm.utils.I_UMessage; -import mmm.utils.URegistry; +import mmm.utils.UNetwork; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.entity.Entity; import net.minecraft.entity.passive.EntityAnimal; @@ -175,7 +175,7 @@ public class FMilkable } final Entity entity = event.getTarget( ); if ( entity.hasCapability( FMilkable.CAPABILITY , null ) ) { - URegistry.network.sendTo( new Message( entity ) , (EntityPlayerMP) player ); + UNetwork.sendTo( new Message( entity ) , (EntityPlayerMP) player ); } } @@ -229,7 +229,7 @@ public class FMilkable // Send updates to players if ( !player.worldObj.isRemote ) { - URegistry.network.sendToAll( new Message( animal ) ); + UNetwork.sendToAll( new Message( animal ) ); } // Actually milk the animal @@ -252,7 +252,7 @@ public class FMilkable { CapabilityManager.INSTANCE.register( FMilkable.class , new Storage( ) , FMilkable::new ); MinecraftForge.EVENT_BUS.register( new EventHandler( ) ); - URegistry.addClientMessage( Message.class ); + UNetwork.addClientMessage( Message.class ); } // ************************************************************************************************************* diff --git a/src/java/mmm/tech/base/TechBase.java b/src/java/mmm/tech/base/TechBase.java index adb742c..5d38024 100644 --- a/src/java/mmm/tech/base/TechBase.java +++ b/src/java/mmm/tech/base/TechBase.java @@ -2,7 +2,7 @@ package mmm.tech.base; import mmm.tech.base.alloy_furnace.AlloyFurnace; -import mmm.utils.URegistry; +import mmm.utils.UNetwork; @@ -11,7 +11,7 @@ public class TechBase public static final AlloyFurnace ALLOY_FURNACE; static { - URegistry.addServerMessage( TBActivationModeMessage.class ); + UNetwork.addServerMessage( TBActivationModeMessage.class ); ALLOY_FURNACE = AlloyFurnace.INSTANCE; } diff --git a/src/java/mmm/tech/base/alloy_furnace/AlloyFurnace.java b/src/java/mmm/tech/base/alloy_furnace/AlloyFurnace.java index e4ac24b..7eaa5d9 100644 --- a/src/java/mmm/tech/base/alloy_furnace/AlloyFurnace.java +++ b/src/java/mmm/tech/base/alloy_furnace/AlloyFurnace.java @@ -3,6 +3,7 @@ package mmm.tech.base.alloy_furnace; import mmm.Mmm; import mmm.tech.base.TBAlloyFurnaceGuiHandler; +import mmm.utils.UNetwork; import mmm.utils.URegistry; import net.minecraft.item.Item; import net.minecraft.item.ItemBlockSpecial; @@ -35,7 +36,7 @@ public enum AlloyFurnace { GameRegistry.registerTileEntity( TBAFTileEntity.class , "mmm:tech/base/alloy_furnace" ); // FIXME NetworkRegistry.INSTANCE.registerGuiHandler( Mmm.get( ) , new TBAlloyFurnaceGuiHandler( ) ); - URegistry.addServerMessage( TBAFMessage.class ); + UNetwork.addServerMessage( TBAFMessage.class ); } } diff --git a/src/java/mmm/tech/base/alloy_furnace/TBAFContainer.java b/src/java/mmm/tech/base/alloy_furnace/TBAFContainer.java index ce2156f..af6de17 100644 --- a/src/java/mmm/tech/base/alloy_furnace/TBAFContainer.java +++ b/src/java/mmm/tech/base/alloy_furnace/TBAFContainer.java @@ -7,7 +7,7 @@ import mmm.tech.base.I_TBConfigurableActivation; import mmm.tech.base.TBActivationModeMessage; import mmm.utils.UInventoryDisplay; import mmm.utils.UInventoryGrid; -import mmm.utils.URegistry; +import mmm.utils.UNetwork; import mmm.utils.gui.UGContainer; import mmm.utils.gui.UGSlotDisplay; import mmm.utils.gui.UGSlotFuel; @@ -172,7 +172,7 @@ public class TBAFContainer if ( te instanceof TBAFTileEntity ) { ( (TBAFTileEntity) te ).setActivationMode( mode ); } - URegistry.network.sendToServer( new TBActivationModeMessage( mode ) ); + UNetwork.sendToServer( new TBActivationModeMessage( mode ) ); } else { this.tileEntity.setActivationMode( mode ); this.tileEntity.forceUpdate( ); @@ -196,7 +196,7 @@ public class TBAFContainer final TileEntity te = this.world.getTileEntity( this.position ); if ( te instanceof TBAFTileEntity ) { ( (TBAFTileEntity) te ).flags = flags; - URegistry.network.sendToServer( new TBAFMessage( flags ) ); + UNetwork.sendToServer( new TBAFMessage( flags ) ); } } else { this.tileEntity.flags = flags; diff --git a/src/java/mmm/tech/base/alloy_furnace/TBAFGui.java b/src/java/mmm/tech/base/alloy_furnace/TBAFGui.java index 3312c88..aab857d 100644 --- a/src/java/mmm/tech/base/alloy_furnace/TBAFGui.java +++ b/src/java/mmm/tech/base/alloy_furnace/TBAFGui.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import mmm.Mmm; import mmm.materials.MAlloyRecipe; -import mmm.utils.URegistry; +import mmm.utils.UNetwork; import mmm.utils.gui.A_UGTab; import mmm.utils.gui.A_UGTabbedContainerScreen; import mmm.utils.gui.UGArrowButton; @@ -176,7 +176,7 @@ public class TBAFGui gui.setRecipe( gui.currentRecipe - 1 ); } else if ( button == this.bConfirm ) { final MAlloyRecipe recipe = gui.recipes.get( gui.currentRecipe ); - URegistry.network.sendToServer( new TBAFMessage( recipe.name , true ) ); + UNetwork.sendToServer( new TBAFMessage( recipe.name , true ) ); gui.container.tileEntity.recipe = recipe; } else { return false; @@ -353,7 +353,7 @@ public class TBAFGui } this.container.setCurrentRecipe( rName , false ); - URegistry.network.sendToServer( new TBAFMessage( rName , false ) ); + UNetwork.sendToServer( new TBAFMessage( rName , false ) ); ( (RecipeTab) this.tabs[ 1 ] ).enableButtons( ); } diff --git a/src/java/mmm/utils/UNetwork.java b/src/java/mmm/utils/UNetwork.java new file mode 100644 index 0000000..a99f037 --- /dev/null +++ b/src/java/mmm/utils/UNetwork.java @@ -0,0 +1,61 @@ +package mmm.utils; + + +import mmm.Mmm; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.IThreadListener; +import net.minecraft.world.WorldServer; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; +import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import net.minecraftforge.fml.relauncher.Side; + + + +public class UNetwork +{ + private static final SimpleNetworkWrapper NET = new SimpleNetworkWrapper( Mmm.ID ); + private static int nextPacketDiscriminator = 0; + + + public static void addClientMessage( final Class< ? extends I_UMessage > message ) + { + UNetwork.NET.registerMessage( // + ( final I_UMessage m , final MessageContext ctx ) -> { + final IThreadListener main = Minecraft.getMinecraft( ); + main.addScheduledTask( ( ) -> m.handleOnClient( Minecraft.getMinecraft( ).thePlayer ) ); + return null; + } , // + message , UNetwork.nextPacketDiscriminator++ , Side.CLIENT ); + } + + + public static void addServerMessage( final Class< ? extends I_UMessage > message ) + { + UNetwork.NET.registerMessage( // + ( final I_UMessage m , final MessageContext ctx ) -> { + final IThreadListener main = (WorldServer) ctx.getServerHandler( ).playerEntity.worldObj; + main.addScheduledTask( ( ) -> m.handleOnServer( ctx.getServerHandler( ).playerEntity ) ); + return null; + } , // + message , UNetwork.nextPacketDiscriminator++ , Side.SERVER ); + } + + + public static void sendToServer( final I_UMessage msg ) + { + UNetwork.NET.sendToServer( msg ); + } + + + public static void sendTo( final I_UMessage msg , final EntityPlayerMP player ) + { + UNetwork.NET.sendTo( msg , player ); + } + + + public static void sendToAll( final I_UMessage msg ) + { + UNetwork.NET.sendToAll( msg ); + } +} diff --git a/src/java/mmm/utils/URegistry.java b/src/java/mmm/utils/URegistry.java index c2cc5e1..a031646 100644 --- a/src/java/mmm/utils/URegistry.java +++ b/src/java/mmm/utils/URegistry.java @@ -9,21 +9,15 @@ import java.util.Map; import mmm.Mmm; import net.minecraft.block.Block; -import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; -import net.minecraft.util.IThreadListener; import net.minecraft.util.ResourceLocation; -import net.minecraft.world.WorldServer; import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.fml.common.IFuelHandler; -import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; -import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.registry.IForgeRegistryEntry; -import net.minecraftforge.fml.relauncher.Side; import scala.actors.threadpool.Arrays; @@ -51,9 +45,6 @@ public class URegistry } - public static final SimpleNetworkWrapper network = new SimpleNetworkWrapper( Mmm.ID ); - private static int nextPacketDiscriminator = 0; - private static final HashSet< I_URecipeRegistrar > RECIPE_REGISTRARS = new HashSet<>( ); private static final HashSet< I_UOreGenerationRegistrar > ORE_GEN_REGISTRARS = new HashSet<>( ); private static final HashMap< Item , Boolean > ITEMS = new HashMap< Item , Boolean >( ); @@ -204,30 +195,6 @@ public class URegistry } - public static void addClientMessage( final Class< ? extends I_UMessage > message ) - { - URegistry.network.registerMessage( // - ( final I_UMessage m , final MessageContext ctx ) -> { - final IThreadListener main = Minecraft.getMinecraft( ); - main.addScheduledTask( ( ) -> m.handleOnClient( Minecraft.getMinecraft( ).thePlayer ) ); - return null; - } , // - message , URegistry.nextPacketDiscriminator++ , Side.CLIENT ); - } - - - public static void addServerMessage( final Class< ? extends I_UMessage > message ) - { - URegistry.network.registerMessage( // - ( final I_UMessage m , final MessageContext ctx ) -> { - final IThreadListener main = (WorldServer) ctx.getServerHandler( ).playerEntity.worldObj; - main.addScheduledTask( ( ) -> m.handleOnServer( ctx.getServerHandler( ).playerEntity ) ); - return null; - } , // - message , URegistry.nextPacketDiscriminator++ , Side.SERVER ); - } - - public static void setupItemModels( ) { for ( final Map.Entry< Item , Boolean > entry : URegistry.ITEMS.entrySet( ) ) {