Updated Networking code (still broken on Client Side only)
This commit is contained in:
@@ -11,11 +11,9 @@ import net.minecraft.world.World;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import net.yseven.findyourway.item.ItemCompassBase;
|
||||
import net.yseven.findyourway.item.ModItems;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AngleGetter implements IItemPropertyGetter {
|
||||
@SideOnly(Side.CLIENT)
|
||||
@@ -71,7 +69,12 @@ public class AngleGetter implements IItemPropertyGetter {
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
private double getAngle(World world, Entity entity, ItemStack stack) {
|
||||
if(blockPos != null) {
|
||||
return Math.atan2((double) blockPos.getZ() - entity.posZ, (double) blockPos.getX() - entity.posX);
|
||||
} else {
|
||||
return (double) 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
|
@@ -18,7 +18,6 @@ import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import net.yseven.findyourway.CommonProxy;
|
||||
import net.yseven.findyourway.FindYourWay;
|
||||
import net.yseven.findyourway.Network.PacketHandler;
|
||||
@@ -51,10 +50,6 @@ public class ClientProxy extends CommonProxy {
|
||||
compassBase.setStructurePos(pos);
|
||||
}
|
||||
|
||||
public static BlockPos getStructurePos(ItemCompassBase compassBase) {
|
||||
return compassBase.getStructurePos();
|
||||
}
|
||||
|
||||
public static void resetStructurePos(ItemCompassBase compass) {
|
||||
compass.setStructurePos(null);
|
||||
compass.setStructureWorld(getWorld());
|
||||
@@ -78,16 +73,23 @@ public class ClientProxy extends CommonProxy {
|
||||
super.postInit(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerItemRenderer(Item item, int meta, String id) {
|
||||
ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(FindYourWay.modId + ":" + id, "inventory"));
|
||||
@Mod.EventBusSubscriber
|
||||
public static class RegistrationHandler {
|
||||
@SubscribeEvent
|
||||
public static void registerModels(ModelRegistryEvent event) {
|
||||
ModItems.registerModels();
|
||||
}
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@SubscribeEvent
|
||||
public void onModelRegistry(ModelRegistryEvent event) {
|
||||
ModItems.ENDER_COMPASS.addPropertyOverride(new ResourceLocation(ModItems.ENDER_COMPASS.assetTag), new AngleGetter(ModItems.ENDER_COMPASS));
|
||||
ModelLoader.setCustomModelResourceLocation(ModItems.ENDER_COMPASS, 0, new ModelResourceLocation("findyourway:ender_compass", "inventory"));
|
||||
registerModel(ModItems.ENDER_COMPASS);
|
||||
registerModel(ModItems.VILLAGE_COMPASS);
|
||||
}
|
||||
|
||||
private void registerModel(ItemCompassBase compass) {
|
||||
compass.addPropertyOverride(new ResourceLocation(compass.assetTag), new AngleGetter(compass));
|
||||
ModelLoader.setCustomModelResourceLocation(compass, 0, new ModelResourceLocation(FindYourWay.modId + ":" + compass.getUnlocalizedName(), "inventory"));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
|
@@ -3,12 +3,15 @@ package net.yseven.findyourway;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.yseven.findyourway.Network.PacketHandler;
|
||||
import net.yseven.findyourway.item.ItemCompassBase;
|
||||
import net.yseven.findyourway.item.ModItems;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -18,7 +21,8 @@ public class CommonProxy {
|
||||
public static ArrayList<ItemCompassBase> compassList = new ArrayList<>();
|
||||
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
PacketHandler.registerMessages("findyourway");
|
||||
ModItems.init();
|
||||
PacketHandler.registerMessages(FindYourWay.modId);
|
||||
}
|
||||
|
||||
public void init(FMLInitializationEvent event) {
|
||||
@@ -29,7 +33,12 @@ public class CommonProxy {
|
||||
|
||||
}
|
||||
|
||||
public void registerItemRenderer(Item item, int meta, String id) {
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerItems(RegistryEvent.Register<Item> event) {
|
||||
event.getRegistry().registerAll(
|
||||
ModItems.ENDER_COMPASS
|
||||
);
|
||||
}
|
||||
|
||||
public static boolean containsCompass(IInventory inventory, ItemCompassBase compass) {
|
||||
@@ -41,4 +50,22 @@ public class CommonProxy {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int setCompassId(ItemCompassBase compass) {
|
||||
switch (compass.getStructureType()){
|
||||
case "Stronghold": return 1;
|
||||
case "Village": return 2;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemCompassBase getCompassId(int id) {
|
||||
switch (id) {
|
||||
case 1: return ModItems.ENDER_COMPASS;
|
||||
case 2: return ModItems.VILLAGE_COMPASS;
|
||||
default:
|
||||
ItemCompassBase ERROR_COMPASS = new ItemCompassBase("error", "");
|
||||
return ERROR_COMPASS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,12 @@
|
||||
package net.yseven.findyourway;
|
||||
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraftforge.client.event.ModelRegistryEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.SidedProxy;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.yseven.findyourway.Network.PacketSendKey;
|
||||
import net.yseven.findyourway.item.ModItems;
|
||||
|
||||
@Mod(modid = FindYourWay.modId, name = FindYourWay.name, version = FindYourWay.version)
|
||||
public class FindYourWay {
|
||||
@@ -33,6 +24,7 @@ public class FindYourWay {
|
||||
@Mod.EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
System.out.println(name + " is loading!");
|
||||
System.out.println("FIND YOUR WAY TEST LINE -------------------------------------- TEST");
|
||||
|
||||
proxy.preInit(event);
|
||||
}
|
||||
@@ -46,17 +38,4 @@ public class FindYourWay {
|
||||
public void postInit(FMLPostInitializationEvent event) {
|
||||
proxy.postInit(event);
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber
|
||||
public static class RegistrationHandler {
|
||||
@SubscribeEvent
|
||||
public static void registerItems(RegistryEvent.Register<Item> event) {
|
||||
ModItems.register(event.getRegistry());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerModels(ModelRegistryEvent event) {
|
||||
ModItems.registerModels();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
import net.yseven.findyourway.Client.ClientProxy;
|
||||
import net.yseven.findyourway.CommonProxy;
|
||||
import net.yseven.findyourway.item.ItemCompassBase;
|
||||
|
||||
public class PacketGetKey implements IMessage, IMessageHandler<PacketGetKey, IMessage> {
|
||||
@@ -14,19 +15,21 @@ public class PacketGetKey implements IMessage, IMessageHandler<PacketGetKey, IMe
|
||||
|
||||
public PacketGetKey() {}
|
||||
|
||||
public PacketGetKey(BlockPos pos, ItemCompassBase compassBase) {
|
||||
public PacketGetKey(BlockPos pos, int id) {
|
||||
structurePos = pos;
|
||||
compass = compassBase;
|
||||
compass = CommonProxy.getCompassId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeLong(structurePos.toLong());
|
||||
buf.writeInt(CommonProxy.setCompassId(compass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
structurePos = BlockPos.fromLong(buf.readLong());
|
||||
compass = CommonProxy.getCompassId(buf.readInt());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -4,10 +4,10 @@ import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
import net.yseven.findyourway.CommonProxy;
|
||||
import net.yseven.findyourway.item.ItemCompassBase;
|
||||
|
||||
public class PacketSendKey implements IMessage {
|
||||
@@ -15,14 +15,10 @@ public class PacketSendKey implements IMessage {
|
||||
private final ItemCompassBase compass;
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
structurePos = BlockPos.fromLong(buf.readLong());
|
||||
}
|
||||
public void fromBytes(ByteBuf buf) {}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeLong(structurePos.toLong());
|
||||
}
|
||||
public void toBytes(ByteBuf buf) {}
|
||||
|
||||
public PacketSendKey(ItemCompassBase compassBase) {
|
||||
compass = compassBase;
|
||||
@@ -31,20 +27,22 @@ public class PacketSendKey implements IMessage {
|
||||
public static class Handler implements IMessageHandler<PacketSendKey, IMessage> {
|
||||
@Override
|
||||
public IMessage onMessage(PacketSendKey message, MessageContext ctx) {
|
||||
FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
|
||||
final EntityPlayerMP player = ctx.getServerHandler().player;
|
||||
if (CommonProxy.containsCompass(player.inventory, message.compass)) {
|
||||
final WorldServer world = (WorldServer) player.world;
|
||||
final int compassId = CommonProxy.setCompassId(message.compass);
|
||||
final String structureType = message.compass.getStructureType();
|
||||
world.addScheduledTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
BlockPos pos = world.getChunkProvider().getNearestStructurePos(world, structureType, new BlockPos(player), true);
|
||||
if (pos != null) {
|
||||
PacketHandler.INSTANCE.sendTo(new PacketGetKey(message.structurePos, compassId), player);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void handle(PacketSendKey message, MessageContext ctx) {
|
||||
//server code
|
||||
EntityPlayerMP player = ctx.getServerHandler().player;
|
||||
WorldServer world = (WorldServer) player.world;
|
||||
if (message.compass != null) {
|
||||
message.structurePos = world.getChunkProvider().getNearestStructurePos(world, message.compass.getStructureType(), new BlockPos(player), false);
|
||||
if (message.structurePos != null) {
|
||||
PacketHandler.INSTANCE.sendTo(message, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package net.yseven.findyourway.item;
|
||||
|
||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
@@ -9,6 +10,9 @@ import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import net.yseven.findyourway.Client.ClientProxy;
|
||||
import net.yseven.findyourway.CommonProxy;
|
||||
import net.yseven.findyourway.FindYourWay;
|
||||
@@ -21,13 +25,13 @@ public class ItemCompassBase extends Item {
|
||||
private World structureWorld;
|
||||
public final String assetTag;
|
||||
|
||||
ItemCompassBase(String name, String structureType, CreativeTabs tab) {
|
||||
this.setUnlocalizedName(name);
|
||||
this.setRegistryName(name);
|
||||
this.structureType = structureType;
|
||||
this.setCreativeTab(tab);
|
||||
this.setMaxStackSize(1);
|
||||
this.assetTag = name + "_angle";
|
||||
public ItemCompassBase(String name, String structureName) {
|
||||
setUnlocalizedName(FindYourWay.modId + "." + name);
|
||||
setRegistryName(name);
|
||||
structureType = structureName;
|
||||
setCreativeTab(CreativeTabs.TOOLS);
|
||||
setMaxStackSize(1);
|
||||
assetTag = name + "_angle";
|
||||
CommonProxy.compassList.add(this);
|
||||
}
|
||||
|
||||
@@ -52,7 +56,7 @@ public class ItemCompassBase extends Item {
|
||||
}
|
||||
|
||||
public void registerItemModel() {
|
||||
FindYourWay.proxy.registerItemRenderer(this, 0, this.getUnlocalizedName());
|
||||
ModelLoader.setCustomModelResourceLocation(this, 0, new ModelResourceLocation(getRegistryName(), "inventory"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,20 +1,20 @@
|
||||
package net.yseven.findyourway.item;
|
||||
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraftforge.registries.IForgeRegistry;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
public class ModItems {
|
||||
public static ItemCompassBase ENDER_COMPASS;
|
||||
public static ItemCompassBase VILLAGE_COMPASS;
|
||||
|
||||
public static ItemCompassBase ENDER_COMPASS = new ItemCompassBase("ender_compass", "Stronghold", CreativeTabs.TOOLS);
|
||||
|
||||
public static void register(IForgeRegistry<Item> registry) {
|
||||
registry.registerAll(
|
||||
ENDER_COMPASS
|
||||
);
|
||||
public static void init(){
|
||||
ENDER_COMPASS = new ItemCompassBase("ender_compass", "Stronghold");
|
||||
VILLAGE_COMPASS = new ItemCompassBase("village_compass", "Village");
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static void registerModels() {
|
||||
ENDER_COMPASS.registerItemModel();
|
||||
VILLAGE_COMPASS.registerItemModel();
|
||||
}
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@
|
||||
"description": "Provides compasses to structures",
|
||||
"version": "1.0.0.0-beta",
|
||||
"mcversion": "1.12.2",
|
||||
"logoFile": "assets/endercompass/textures/logo.png",
|
||||
"logoFile": "assets/findyourway/textures/logo.png",
|
||||
"url": "minecraftforge.net/",
|
||||
"updateJSON": "minecraftforge.net/versions.json",
|
||||
"authorList": ["Yavin7"],
|
||||
|
Reference in New Issue
Block a user