Upload working sources

This commit is contained in:
Yavin7
2018-10-15 14:45:56 -05:00
parent f3a99dc82f
commit 536e11fa8e
85 changed files with 1136 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
package net.yseven.findyourway.Client;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItemFrame;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
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)
private double prevAngle = 0.0D;
@SideOnly(Side.CLIENT)
private double prevWobble = 0.0D;
@SideOnly(Side.CLIENT)
private long prevWorldTime = 0L;
double blockX;
double blockZ;
@Override
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
if (entityIn == null && !stack.isOnItemFrame()) return 0.0F;
final boolean entityExists = entityIn != null;
final Entity entity = (Entity) (entityExists ? entityIn : stack.getItemFrame());
if (worldIn == null) worldIn = entity.world;
double rotation = entityExists ? (double) entity.rotationYaw : getFrameAngle((EntityItemFrame) entity);
rotation %= 360.0D;
double adjusted = Math.PI - ((rotation - 90.0D) * 0.01745329238474369D - getAngle(worldIn, entity, stack));
if (entityExists) adjusted = wobble(worldIn, adjusted);
final float f = (float) (adjusted / (Math.PI * 2D));
return MathHelper.positiveModulo(f, 1.0F);
}
@SideOnly(Side.CLIENT)
private double wobble(World world, double angle) {
long worldTime = world.getTotalWorldTime();
if (worldTime != prevWorldTime) {
prevWorldTime = worldTime;
double angleDifference = angle - prevAngle;
angleDifference = MathHelper.positiveModulo(angleDifference + 0.5D, 1.0D) - 0.5D;
prevWobble += angleDifference * 0.1D;
prevWobble *= 0.8D;
prevAngle = MathHelper.positiveModulo(prevAngle + prevWobble, 1.0D);
}
return prevAngle;
}
@SideOnly(Side.CLIENT)
private double getAngle(World world, Entity entity, ItemStack stack) {
return Math.atan2((double) blockZ - entity.posZ, (double) blockX - entity.posX);
}
@SideOnly(Side.CLIENT)
private double getFrameAngle(EntityItemFrame entity) {
return (double) MathHelper.wrapDegrees(180 + (entity.facingDirection.getHorizontalIndex() * 90));
}
@SideOnly(Side.CLIENT)
private double getPosToAngle(BlockPos pos, Entity entity) {
return Math.atan2((double)pos.getZ() - entity.posZ, (double)pos.getX() - entity.posX);
}
}

View File

@@ -0,0 +1,101 @@
package net.yseven.findyourway.Client;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.client.FMLClientHandler;
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.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.yseven.findyourway.CommonProxy;
import net.yseven.findyourway.FindYourWay;
import net.yseven.findyourway.Network.PacketHandler;
import net.yseven.findyourway.Network.PacketSendKey;
import net.yseven.findyourway.item.ItemCompassBase;
import net.yseven.findyourway.item.ModItems;
@Mod.EventBusSubscriber(Side.CLIENT)
public class ClientProxy extends CommonProxy {
//Get information about the player and world
public static Minecraft getMinecraft() {
return FMLClientHandler.instance().getClient();
}
public static WorldClient getWorld() {
return getMinecraft().world;
}
public static EntityPlayerSP getPlayer() {
return getMinecraft().player;
}
//TO-DO fill out the following methods
public static boolean hasCompass(ItemCompassBase compass) {
return getPlayer() != null && CommonProxy.containsCompass(getPlayer().inventory, compass);
}
public static void setStructurePos(ItemCompassBase compassBase, BlockPos pos) {
compassBase.setStructurePos(pos);
}
public static BlockPos getStructurePos(ItemCompassBase compassBase) {
return compassBase.getStructurePos();
}
public static void resetStructurePos(ItemCompassBase compass) {
compass.setStructurePos(null);
compass.setStructureWorld(getWorld());
PacketHandler.INSTANCE.sendToServer(new PacketSendKey(compass));
}
//Proxy Info
@Override
public void preInit(FMLPreInitializationEvent event) {
super.preInit(event);
MinecraftForge.EVENT_BUS.register(this);
}
@Override
public void init(FMLInitializationEvent event) {
super.init(event);
}
@Override
public void postInit(FMLPostInitializationEvent event) {
super.postInit(event);
}
@Override
public void registerItemRenderer(Item item, int meta, String id) {
ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(FindYourWay.modId + ":" + id, "inventory"));
}
@SubscribeEvent
public void onModelRegistry(ModelRegistryEvent event) {
ModItems.ENDER_COMPASS.addPropertyOverride(new ResourceLocation(ModItems.ENDER_COMPASS.assetTag), new AngleGetter());
ModelLoader.setCustomModelResourceLocation(ModItems.ENDER_COMPASS, 0, new ModelResourceLocation("findyourway:ender_compass", "inventory"));
}
@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event) {
for(int i = 0; i < CommonProxy.compassList.size(); i++) {
if(hasCompass(CommonProxy.compassList.get(i))) {
if(getWorld() != CommonProxy.compassList.get(i).getStructureWorld()) {
resetStructurePos(CommonProxy.compassList.get(i));
}
}
}
}
}

View File

@@ -0,0 +1,44 @@
package net.yseven.findyourway;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
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.yseven.findyourway.Network.PacketHandler;
import net.yseven.findyourway.item.ItemCompassBase;
import java.util.ArrayList;
@Mod.EventBusSubscriber
public class CommonProxy {
public static ArrayList<ItemCompassBase> compassList = new ArrayList<>();
public void preInit(FMLPreInitializationEvent event) {
PacketHandler.registerMessages("findyourway");
}
public void init(FMLInitializationEvent event) {
}
public void postInit(FMLPostInitializationEvent event) {
}
public void registerItemRenderer(Item item, int meta, String id) {
}
public static boolean containsCompass(IInventory inventory, ItemCompassBase compass) {
for(int slot = 0; slot < inventory.getSizeInventory(); slot++) {
ItemStack stack = inventory.getStackInSlot(slot);
if(!stack.isEmpty() && stack.getItem() == compass) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,62 @@
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 {
public static final String modId = "findyourway";
public static final String name = "Find Your Way";
public static final String version = "1.0.0-beta";
@Mod.Instance(modId)
public static FindYourWay instance;
@SidedProxy(serverSide = "net.yseven.findyourway.CommonProxy", clientSide = "net.yseven.findyourway.Client.ClientProxy")
public static CommonProxy proxy;
public static SimpleNetworkWrapper network;
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event) {
System.out.println(name + " is loading!");
proxy.preInit(event);
}
@Mod.EventHandler
public void init(FMLInitializationEvent event) {
proxy.init(event);
}
@Mod.EventHandler
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();
}
}
}

View File

@@ -0,0 +1,38 @@
package net.yseven.findyourway.Network;
import io.netty.buffer.ByteBuf;
import net.minecraft.util.math.BlockPos;
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.item.ItemCompassBase;
public class PacketGetKey implements IMessage, IMessageHandler<PacketGetKey, IMessage> {
private BlockPos structurePos;
private ItemCompassBase compass;
public PacketGetKey() {}
public PacketGetKey(BlockPos pos, ItemCompassBase compassBase) {
structurePos = pos;
compass = compassBase;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeLong(structurePos.toLong());
}
@Override
public void fromBytes(ByteBuf buf) {
structurePos = BlockPos.fromLong(buf.readLong());
}
@Override
public IMessage onMessage(PacketGetKey message, MessageContext ctx) {
//Client Code
ClientProxy.getMinecraft().addScheduledTask(() -> ClientProxy.setStructurePos(message.compass, message.structurePos));
return null;
}
}

View File

@@ -0,0 +1,30 @@
package net.yseven.findyourway.Network;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;
@SuppressWarnings("WeakerAccess")
public class PacketHandler {
private static int packetId = 0;
public static SimpleNetworkWrapper INSTANCE = null;
public PacketHandler() {
}
public static int nextID() {
return packetId++;
}
public static void registerMessages(String channelName) {
INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(channelName);
registerMessages();
}
public static void registerMessages() {
// Register messages which are sent from the client to the server here:
INSTANCE.registerMessage(PacketSendKey.Handler.class, PacketSendKey.class, nextID(), Side.SERVER);
INSTANCE.registerMessage(PacketGetKey.class, PacketGetKey.class, nextID(), Side.CLIENT);
}
}

View File

@@ -0,0 +1,50 @@
package net.yseven.findyourway.Network;
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.item.ItemCompassBase;
public class PacketSendKey implements IMessage {
private BlockPos structurePos;
private final ItemCompassBase compass;
@Override
public void fromBytes(ByteBuf buf) {
structurePos = BlockPos.fromLong(buf.readLong());
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeLong(structurePos.toLong());
}
public PacketSendKey(ItemCompassBase compassBase) {
compass = compassBase;
}
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));
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);
}
}
}
}
}

View File

@@ -0,0 +1,17 @@
package net.yseven.findyourway;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class Util {
public static boolean checkInventoryForCompass(InventoryPlayer inventory, Item compass) {
for(int x = 0; x < inventory.getSizeInventory(); x++) {
ItemStack stack = inventory.getStackInSlot(x);
if(!stack.isEmpty() && stack.getItem() == compass) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,65 @@
package net.yseven.findyourway.item;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.yseven.findyourway.Client.ClientProxy;
import net.yseven.findyourway.CommonProxy;
import net.yseven.findyourway.FindYourWay;
import javax.annotation.Nonnull;
public class ItemCompassBase extends Item {
private final String structureType;
private BlockPos structurePos;
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";
CommonProxy.compassList.add(this);
}
public String getStructureType() {
return structureType;
}
public void setStructurePos(BlockPos pos) {
structurePos = pos;
}
public BlockPos getStructurePos() {
return structurePos;
}
public void setStructureWorld(World world) {
structureWorld = world;
}
public World getStructureWorld() {
return structureWorld;
}
public void registerItemModel() {
FindYourWay.proxy.registerItemRenderer(this, 0, this.getUnlocalizedName());
}
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
if (world.isRemote) {
ClientProxy.resetStructurePos(this);
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
}

View File

@@ -0,0 +1,20 @@
package net.yseven.findyourway.item;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraftforge.registries.IForgeRegistry;
public class ModItems {
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 registerModels() {
ENDER_COMPASS.registerItemModel();
}
}