diff --git a/src/main/java/tschipp/carryon/client/event/RenderEntityEvents.java b/src/main/java/tschipp/carryon/client/event/RenderEntityEvents.java new file mode 100644 index 0000000..873866c --- /dev/null +++ b/src/main/java/tschipp/carryon/client/event/RenderEntityEvents.java @@ -0,0 +1,338 @@ +package tschipp.carryon.client.event; + +import java.lang.reflect.Field; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.AbstractClientPlayer; +import net.minecraft.client.entity.EntityPlayerSP; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.model.ModelPlayer; +import net.minecraft.client.model.ModelRenderer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.OpenGlHelper; +import net.minecraft.client.renderer.entity.RenderManager; +import net.minecraft.client.renderer.entity.RenderPlayer; +import net.minecraft.client.resources.DefaultPlayerSkin; +import net.minecraft.client.settings.GameSettings; +import net.minecraft.client.settings.KeyBinding; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumHand; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.client.event.GuiOpenEvent; +import net.minecraftforge.client.event.MouseEvent; +import net.minecraftforge.client.event.RenderHandEvent; +import net.minecraftforge.client.event.RenderPlayerEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.InputEvent; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; +import tschipp.carryon.common.handler.RegistrationHandler; +import tschipp.carryon.common.item.ItemEntity; + +public class RenderEntityEvents +{ + + /* + * Prevents the Player from scrolling + */ + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onScroll(MouseEvent event) + { + if (event.getDwheel() > 0 || event.getDwheel() < 0) + { + ItemStack stack = Minecraft.getMinecraft().player.getHeldItemMainhand(); + if (!stack.isEmpty() && stack.getItem() == RegistrationHandler.itemEntity) + { + if (ItemEntity.hasEntityData(stack)) + event.setCanceled(true); + } + } + } + + /* + * Prevents the Player from opening Guis + */ + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onGuiOpen(GuiOpenEvent event) + { + if (event.getGui() != null) + { + boolean inventory = event.getGui() instanceof GuiContainer; + EntityPlayer player = Minecraft.getMinecraft().player; + if (player != null) + { + ItemStack stack = player.getHeldItem(EnumHand.MAIN_HAND); + if (inventory && !stack.isEmpty() && stack.getItem() == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(stack)) + { + event.setCanceled(true); + Minecraft.getMinecraft().currentScreen = null; + } + } + } + } + + /* + * Prevents the Player from switching Slots + */ + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void inputEvent(InputEvent event) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException + { + GameSettings settings = Minecraft.getMinecraft().gameSettings; + Field field = KeyBinding.class.getDeclaredFields()[7]; + field.setAccessible(true); + ItemStack stack = Minecraft.getMinecraft().player.getHeldItemMainhand(); + if (!stack.isEmpty() && stack.getItem() == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(stack)) + { + if (settings.keyBindDrop.isPressed()) + { + field.set(settings.keyBindDrop, false); + } + if (settings.keyBindSwapHands.isPressed()) + { + field.set(settings.keyBindSwapHands, false); + } + for (KeyBinding keyBind : settings.keyBindsHotbar) + { + if (keyBind.isPressed()) + { + field.set(keyBind, false); + } + } + } + } + + /* + * Renders the Entity in First Person + */ + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void renderHand(RenderHandEvent event) + { + World world = Minecraft.getMinecraft().world; + EntityPlayer player = Minecraft.getMinecraft().player; + AbstractClientPlayer aplayer = (AbstractClientPlayer) player; + ItemStack stack = player.getHeldItemMainhand(); + int perspective = Minecraft.getMinecraft().gameSettings.thirdPersonView; + float partialticks = event.getPartialTicks(); + + if (!stack.isEmpty() && stack.getItem() == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(stack)) + { + BlockPos pos = player.getPosition(); + Entity entity = ItemEntity.getEntity(stack, world); + if (entity != null) + { + double d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialticks; + double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialticks; + double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialticks; + + entity.setPosition(d0, d1, d2); + entity.rotationYaw = 0.0f; + entity.prevRotationYaw = 0.0f; + entity.setRotationYawHead(0.0f); + + float height = entity.height; + float width = entity.width; + float multiplier = height * width; + + GlStateManager.pushMatrix(); + GlStateManager.scale(1, 1, 1); + GlStateManager.rotate(180, 0, 1, 0); + GlStateManager.translate(0.0, -height, width + 0.1); + GlStateManager.disableLighting(); + GlStateManager.enableAlpha(); + GlStateManager.color(0, 0, 0, 0); + + if (perspective == 0) + Minecraft.getMinecraft().getRenderManager().renderEntityStatic(entity, 0.0f, false); + + GlStateManager.enableLighting(); + GlStateManager.disableAlpha(); + GlStateManager.scale(1, 1, 1); + GlStateManager.popMatrix(); + + if (perspective == 0) + event.setCanceled(true); + } + } + else + { + event.setCanceled(false); + Minecraft mc = Minecraft.getMinecraft(); + RenderManager manager = mc.getRenderManager(); + RenderPlayer renderPlayer = manager.getSkinMap().get(aplayer.getSkinType()); + ModelPlayer modelPlayer = renderPlayer.getMainModel(); + modelPlayer.bipedLeftArm.isHidden = false; + modelPlayer.bipedRightArm.isHidden = false; + } + } + + /* + * Renders the Block in Third Person + */ + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onPlayerRenderPost(RenderPlayerEvent.Post event) + { + World world = Minecraft.getMinecraft().world; + EntityPlayer player = event.getEntityPlayer(); + ModelPlayer modelPlayer = event.getRenderer().getMainModel(); + EntityPlayerSP clientPlayer = Minecraft.getMinecraft().player; + ItemStack stack = player.getHeldItemMainhand(); + float partialticks = event.getPartialRenderTick(); + if (!stack.isEmpty() && stack.getItem() == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(stack)) + { + Entity entity = ItemEntity.getEntity(stack, world); + + float rotation = -player.renderYawOffset; + int perspective = Minecraft.getMinecraft().gameSettings.thirdPersonView; + + if (entity != null) + { + double d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialticks; + double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialticks; + double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialticks; + + double c0 = clientPlayer.lastTickPosX + (clientPlayer.posX - clientPlayer.lastTickPosX) * (double) partialticks; + double c1 = clientPlayer.lastTickPosY + (clientPlayer.posY - clientPlayer.lastTickPosY) * (double) partialticks; + double c2 = clientPlayer.lastTickPosZ + (clientPlayer.posZ - clientPlayer.lastTickPosZ) * (double) partialticks; + + double xOffset = d0 - c0; + double yOffset = d1 - c1; + double zOffset = d2 - d2; + + float height = entity.height; + float width = entity.width; + float multiplier = height * width; + + entity.setPosition(d0, d1, d2); + entity.rotationYaw = 0.0f; + entity.prevRotationYaw = 0.0f; + entity.setRotationYawHead(0.0f); + + GlStateManager.pushMatrix(); + GlStateManager.translate(xOffset, yOffset, zOffset); + GlStateManager.scale((10 - multiplier) * 0.08, (10 - multiplier) * 0.08, (10 - multiplier) * 0.08); + GlStateManager.rotate(rotation, 0, 1f, 0); + GlStateManager.translate(0.0, (height / 2) + -(height / 2) + 1, (width - 0.1) < 0.7 ? (width - 0.1) + (0.7 - (width - 0.1)) : (width - 0.1)); + + if (player.isSneaking()) + GlStateManager.translate(0, -0.3, 0); + + Minecraft.getMinecraft().getRenderManager().renderEntityStatic(entity, 0.0f, false); + + GlStateManager.scale(1, 1, 1); + GlStateManager.popMatrix(); + } + } + else + { + modelPlayer.bipedLeftArm.isHidden = false; + modelPlayer.bipedRightArm.isHidden = false; + } + + } + + /* + * Renders correct arm rotation + */ + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onPlayerRenderPre(RenderPlayerEvent.Pre event) + { + EntityPlayer player = event.getEntityPlayer(); + AbstractClientPlayer aplayer = (AbstractClientPlayer) player; + ItemStack stack = player.getHeldItemMainhand(); + ModelPlayer model = event.getRenderer().getMainModel(); + EntityPlayerSP clientPlayer = Minecraft.getMinecraft().player; + + ResourceLocation skinLoc = DefaultPlayerSkin.getDefaultSkin(player.getPersistentID()); + + ModelRenderer fakeLeftArm = new ModelRenderer(model, 32, 48); + ModelRenderer fakeRightArm = new ModelRenderer(model, 40, 16); + + if (!stack.isEmpty() && stack.getItem() == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(stack)) + { + if (model.bipedBody.childModels != null && !model.bipedBody.childModels.isEmpty()) + model.bipedBody.childModels.clear(); + + model.bipedLeftArm.isHidden = true; + model.bipedRightArm.isHidden = true; + + Minecraft.getMinecraft().getTextureManager().bindTexture(skinLoc); + float rotation = -player.renderYawOffset; + if (aplayer.getSkinType().equals("default")) + { + fakeLeftArm.addBox(model.bipedLeftArm.offsetX + 4.2F, model.bipedLeftArm.offsetY, model.bipedLeftArm.offsetZ, 4, 12, 4, .08F); + } + else + { + fakeLeftArm.addBox(model.bipedLeftArm.offsetX + 4.2F, model.bipedLeftArm.offsetY, model.bipedLeftArm.offsetZ, 3, 12, 4, .08F); + } + + if (aplayer.getSkinType().equals("default")) + { + fakeRightArm.addBox(model.bipedRightArm.offsetX - 7.9F, model.bipedRightArm.offsetY, model.bipedRightArm.offsetZ, 4, 12, 4, .08F); + } + else + { + fakeRightArm.addBox(model.bipedRightArm.offsetX - 7.2F, model.bipedRightArm.offsetY, model.bipedRightArm.offsetZ, 3, 12, 4, .08F); + } + + if (!player.isSneaking()) + { + fakeRightArm.rotateAngleX = -1.2F; + fakeLeftArm.rotateAngleX = -1.2F; + } + else + { + fakeRightArm.rotateAngleX = -1.7F; + fakeLeftArm.rotateAngleX = -1.7F; + } + + fakeRightArm.rotateAngleY = -0.15f; + fakeLeftArm.rotateAngleY = 0.15f; + + model.bipedBody.addChild(fakeLeftArm); + model.bipedBody.addChild(fakeRightArm); + + } + else + { + model.bipedLeftArm.isHidden = false; + model.bipedRightArm.isHidden = false; + if (model.bipedBody.childModels != null && !model.bipedBody.childModels.isEmpty()) + { + model.bipedBody.childModels.clear(); + } + } + + if (stack.isEmpty() || stack.getItem() != RegistrationHandler.itemEntity || !ItemEntity.hasEntityData(stack)) + { + model.bipedLeftArm.isHidden = false; + model.bipedRightArm.isHidden = false; + } + + } + + @SideOnly(Side.CLIENT) + private static RenderPlayer getRenderPlayer(AbstractClientPlayer player) + { + Minecraft mc = Minecraft.getMinecraft(); + RenderManager manager = mc.getRenderManager(); + return manager.getSkinMap().get(player.getSkinType()); + } + + @SideOnly(Side.CLIENT) + private static ModelPlayer getPlayerModel(AbstractClientPlayer player) + { + return getRenderPlayer(player).getMainModel(); + } + +} diff --git a/src/main/java/tschipp/carryon/common/config/CarryOnConfig.java b/src/main/java/tschipp/carryon/common/config/CarryOnConfig.java index f990af3..a52a3ba 100644 --- a/src/main/java/tschipp/carryon/common/config/CarryOnConfig.java +++ b/src/main/java/tschipp/carryon/common/config/CarryOnConfig.java @@ -27,9 +27,15 @@ public class CarryOnConfig { @Config.Comment("Tile Entities that the Player is not allowed to pick up") public static Configs.ForbiddenTiles forbiddenTiles = new Configs.ForbiddenTiles(); + @Config.LangKey(CarryOn.MODID) + @Config.Comment("Entities that the Player is not allowed to pick up") + public static Configs.ForbiddenEntities forbiddenEntities = new Configs.ForbiddenEntities(); + @Config.LangKey(CarryOn.MODID) @Config.Comment("Model Overrides based on NBT or on Meta. Advanced Users Only!") public static Configs.ModelOverrides modelOverrides = new Configs.ModelOverrides(); + + @Mod.EventBusSubscriber public static class EventHandler { diff --git a/src/main/java/tschipp/carryon/common/config/Configs.java b/src/main/java/tschipp/carryon/common/config/Configs.java index 7aa345c..ce15060 100644 --- a/src/main/java/tschipp/carryon/common/config/Configs.java +++ b/src/main/java/tschipp/carryon/common/config/Configs.java @@ -16,10 +16,41 @@ public class Configs { @Comment("Allow all blocks to be picked up, not just Tile Entites") public boolean pickupAllBlocks = false; - @Comment("Maximum distance from where Blocks can be picked up") + @Comment("Whether Blocks and Entities slow the creative player down when carried") + public boolean slownessInCreative = true; + + @Config.RangeDouble(min = 0) + @Comment("Maximum distance from where Blocks and Entities can be picked up") public double maxDistance = 2.5; + + @Config.RangeDouble(min = 0, max = 10) + @Comment("Max width of entities that can be picked up in survival mode") + public float maxEntityWidth = 1.5f; + + @Config.RangeDouble(min = 0, max = 10) + @Comment("Max height of entities that can be picked up in survival mode") + public float maxEntityHeight = 1.5f; + + @Comment("Whether hostile mobs should be able to picked up in survival mode") + public boolean pickupHostileMobs = false; + + @Comment("Larger Entities slow down the player more") + public boolean heavyEntities = true; } + public static class ForbiddenEntities + { + + @Config.RequiresMcRestart() + @Comment("Entities that cannot be picked up") + public String[] forbiddenEntities = new String[] + { + "minecraft:ender_crystal", + "minecraft:ender_dragon" + }; + } + + public static class ForbiddenTiles { @Config.RequiresMcRestart() diff --git a/src/main/java/tschipp/carryon/common/event/ItemEntityEvents.java b/src/main/java/tschipp/carryon/common/event/ItemEntityEvents.java new file mode 100644 index 0000000..5058191 --- /dev/null +++ b/src/main/java/tschipp/carryon/common/event/ItemEntityEvents.java @@ -0,0 +1,99 @@ +package tschipp.carryon.common.event; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EnumCreatureType; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.passive.EntityTameable; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumHand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.event.entity.EntityJoinWorldEvent; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import net.minecraftforge.fml.common.eventhandler.Event.Result; +import net.minecraftforge.fml.common.eventhandler.EventPriority; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import tschipp.carryon.common.config.CarryOnConfig; +import tschipp.carryon.common.handler.ForbiddenTileHandler; +import tschipp.carryon.common.handler.RegistrationHandler; +import tschipp.carryon.common.item.ItemEntity; + +public class ItemEntityEvents +{ + + @SubscribeEvent(priority = EventPriority.HIGH) + public void onBlockClick(PlayerInteractEvent.RightClickBlock event) + { + EntityPlayer player = event.getEntityPlayer(); + ItemStack stack = player.getHeldItemMainhand(); + if (!stack.isEmpty() && stack.getItem() == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(stack)) + { + event.setUseBlock(Result.DENY); + } + + } + + @SubscribeEvent(priority = EventPriority.HIGH) + public void onItemDropped(EntityJoinWorldEvent event) + { + Entity e = event.getEntity(); + World world = event.getWorld(); + if (e instanceof EntityItem) + { + EntityItem eitem = (EntityItem) e; + ItemStack stack = eitem.getEntityItem(); + Item item = stack.getItem(); + if (item == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(stack)) + { + BlockPos pos = eitem.getPosition(); + Entity entity = ItemEntity.getEntity(stack, world); + entity.setPosition(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5); + world.spawnEntity(entity); + + ItemEntity.clearEntityData(stack); + eitem.setEntityItemStack(ItemStack.EMPTY); + } + } + } + + @SubscribeEvent + public void onEntityRightClick(PlayerInteractEvent.EntityInteract event) + { + EntityPlayer player = event.getEntityPlayer(); + ItemStack main = player.getHeldItemMainhand(); + ItemStack off = player.getHeldItemOffhand(); + World world = event.getWorld(); + Entity entity = event.getTarget(); + BlockPos pos = entity.getPosition(); + + if (main.isEmpty() && off.isEmpty() && player.isSneaking()) + { + ItemStack stack = new ItemStack(RegistrationHandler.itemEntity); + + if (!(entity instanceof EntityPlayer) && !ForbiddenTileHandler.isForbidden(entity) && (CarryOnConfig.settings.pickupHostileMobs ? true : !entity.isCreatureType(EnumCreatureType.MONSTER, false) || player.isCreative()) && (entity.height <= CarryOnConfig.settings.maxEntityHeight && entity.width <= CarryOnConfig.settings.maxEntityWidth || player.isCreative())) + { + double distance = pos.distanceSqToCenter(player.posX, player.posY + 0.5, player.posZ); + + if (distance < Math.pow(CarryOnConfig.settings.maxDistance, 2)) + { + if (entity instanceof EntityTameable) + { + EntityTameable tame = (EntityTameable) entity; + if (tame.getOwnerId() != null && tame.getOwnerId() != player.getUUID(player.getGameProfile())) + return; + } + if (ItemEntity.storeEntityData(entity, world, stack)) + { + entity.setDead(); + player.setHeldItem(EnumHand.MAIN_HAND, stack); + } + } + + } + + } + } + +} diff --git a/src/main/java/tschipp/carryon/common/handler/ForbiddenTileHandler.java b/src/main/java/tschipp/carryon/common/handler/ForbiddenTileHandler.java index 1618017..d118e7a 100644 --- a/src/main/java/tschipp/carryon/common/handler/ForbiddenTileHandler.java +++ b/src/main/java/tschipp/carryon/common/handler/ForbiddenTileHandler.java @@ -4,31 +4,45 @@ import java.util.ArrayList; import java.util.List; import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityList; +import net.minecraftforge.fml.common.registry.ForgeRegistries; import tschipp.carryon.common.config.CarryOnConfig; public class ForbiddenTileHandler { public static final List FORBIDDEN_TILES; - + public static final List FORBIDDEN_ENTITIES; + public static boolean isForbidden(Block block) { return FORBIDDEN_TILES.contains(block.getRegistryName().toString()); } - + + public static boolean isForbidden(Entity entity) + { + if (EntityList.getKey(entity) != null) + { + String name = EntityList.getKey(entity).toString(); + boolean contains = FORBIDDEN_ENTITIES.contains(name); + return contains; + } + return true; + } + static { String[] forbidden = CarryOnConfig.forbiddenTiles.forbiddenTiles; - FORBIDDEN_TILES = new ArrayList(); - - for(int i = 0; i < forbidden.length; i++) + + for (int i = 0; i < forbidden.length; i++) { - if(forbidden[i].contains("*")) + if (forbidden[i].contains("*")) { String modid = forbidden[i].replace("*", ""); - for(int k = 0; k < Block.REGISTRY.getKeys().size(); k++) + for (int k = 0; k < Block.REGISTRY.getKeys().size(); k++) { - if(Block.REGISTRY.getKeys().toArray()[k].toString().contains(modid)) + if (Block.REGISTRY.getKeys().toArray()[k].toString().contains(modid)) { FORBIDDEN_TILES.add(Block.REGISTRY.getKeys().toArray()[k].toString()); } @@ -36,5 +50,25 @@ public class ForbiddenTileHandler } FORBIDDEN_TILES.add(forbidden[i]); } + + String[] forbiddenEntity = CarryOnConfig.forbiddenEntities.forbiddenEntities; + FORBIDDEN_ENTITIES = new ArrayList(); + + for (int i = 0; i < forbiddenEntity.length; i++) + { + if (forbiddenEntity[i].contains("*")) + { + String modid = forbiddenEntity[i].replace("*", ""); + for (int k = 0; k < ForgeRegistries.ENTITIES.getKeys().size(); k++) + { + if (ForgeRegistries.ENTITIES.getKeys().toArray()[k].toString().contains(modid)) + { + FORBIDDEN_ENTITIES.add(ForgeRegistries.ENTITIES.getKeys().toArray()[k].toString()); + } + } + } + FORBIDDEN_ENTITIES.add(forbiddenEntity[i]); + } } + } diff --git a/src/main/java/tschipp/carryon/common/handler/RegistrationHandler.java b/src/main/java/tschipp/carryon/common/handler/RegistrationHandler.java index c920ffc..987fe49 100644 --- a/src/main/java/tschipp/carryon/common/handler/RegistrationHandler.java +++ b/src/main/java/tschipp/carryon/common/handler/RegistrationHandler.java @@ -5,32 +5,40 @@ import net.minecraft.item.Item; import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.common.MinecraftForge; import tschipp.carryon.CarryOn; +import tschipp.carryon.client.event.RenderEntityEvents; import tschipp.carryon.client.event.RenderEvents; +import tschipp.carryon.common.event.ItemEntityEvents; import tschipp.carryon.common.event.ItemEvents; +import tschipp.carryon.common.item.ItemEntity; import tschipp.carryon.common.item.ItemTile; public class RegistrationHandler { public static Item itemTile; - + public static Item itemEntity; + public static void regItems() { itemTile = new ItemTile(); + itemEntity = new ItemEntity(); } public static void regItemRenders() { ModelLoader.setCustomModelResourceLocation(itemTile, 0, new ModelResourceLocation(CarryOn.MODID + ":" + "tile", "inventory")); + ModelLoader.setCustomModelResourceLocation(itemEntity, 0, new ModelResourceLocation(CarryOn.MODID + ":" + "tile", "inventory")); } public static void regCommonEvents() { MinecraftForge.EVENT_BUS.register(new ItemEvents()); + MinecraftForge.EVENT_BUS.register(new ItemEntityEvents()); } public static void regClientEvents() { MinecraftForge.EVENT_BUS.register(new RenderEvents()); + MinecraftForge.EVENT_BUS.register(new RenderEntityEvents()); } public static void regOverrideList() @@ -38,5 +46,6 @@ public class RegistrationHandler ModelOverridesHandler.initOverrides(); } + } diff --git a/src/main/java/tschipp/carryon/common/item/ItemEntity.java b/src/main/java/tschipp/carryon/common/item/ItemEntity.java new file mode 100644 index 0000000..91b907b --- /dev/null +++ b/src/main/java/tschipp/carryon/common/item/ItemEntity.java @@ -0,0 +1,203 @@ +package tschipp.carryon.common.item; + +import javax.annotation.Nonnull; + +import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityList; +import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.MobEffects; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.potion.PotionEffect; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.translation.I18n; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.registry.GameRegistry; +import tschipp.carryon.CarryOn; +import tschipp.carryon.common.config.CarryOnConfig; + +public class ItemEntity extends Item +{ + + public static final String ENTITY_DATA_KEY = "entityData"; + + public ItemEntity() + { + this.setUnlocalizedName("tile_entity"); + this.setRegistryName(CarryOn.MODID, "tile_entity"); + GameRegistry.register(this); + this.setMaxStackSize(1); + } + + @Override + public String getItemStackDisplayName(ItemStack stack) + { + if (hasEntityData(stack)) + { + return I18n.translateToLocal(EntityList.getTranslationName(new ResourceLocation(getEntityName(stack)))); + } + + return ""; + } + + public static boolean hasEntityData(ItemStack stack) + { + if (stack.hasTagCompound()) + { + NBTTagCompound tag = stack.getTagCompound(); + return tag.hasKey(ENTITY_DATA_KEY) && tag.hasKey("entity"); + } + return false; + } + + public static boolean storeEntityData(@Nonnull Entity entity, World world, ItemStack stack) + { + if (entity == null) + return false; + + if (stack.isEmpty()) + return false; + + NBTTagCompound entityData = new NBTTagCompound(); + entityData = entity.writeToNBT(entityData); + + String name = EntityList.getKey(entity).toString(); + + NBTTagCompound tag = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound(); + if (tag.hasKey(ENTITY_DATA_KEY)) + return false; + + tag.setTag(ENTITY_DATA_KEY, entityData); + tag.setString("entity", name); + stack.setTagCompound(tag); + return true; + } + + @Override + public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) + { + ItemStack stack = player.getHeldItem(hand); + Block block = world.getBlockState(pos).getBlock(); + + if (hasEntityData(stack)) + { + BlockPos finalPos = pos; + + if (!block.isReplaceable(world, pos)) + { + finalPos = pos.offset(facing); + } + + Entity entity = getEntity(stack, world); + if (entity != null) + { + if (!world.isRemote) + { + entity.setPositionAndRotation(finalPos.getX() + 0.5, finalPos.getY(), finalPos.getZ() + 0.5, 180 + player.rotationYawHead, 0.0f); + world.spawnEntity(entity); + if (entity instanceof EntityLiving) + { + ((EntityLiving) entity).playLivingSound(); + } + clearEntityData(stack); + player.setHeldItem(hand, ItemStack.EMPTY); + } + return EnumActionResult.SUCCESS; + } + } + + return EnumActionResult.FAIL; + } + + @Override + public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) + { + if (hasEntityData(stack)) + { + if(getEntity(stack, world) == null) + stack = ItemStack.EMPTY; + + if (entity instanceof EntityLivingBase) + { + if(entity instanceof EntityPlayer && CarryOnConfig.settings.slownessInCreative ? false : ((EntityPlayer)entity).isCreative()) + return; + + ((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 1, potionLevel(stack, world), false, false)); + } + + } + else + { + stack = ItemStack.EMPTY; + } + } + + public static void clearEntityData(ItemStack stack) + { + if (stack.hasTagCompound()) + { + NBTTagCompound tag = stack.getTagCompound(); + tag.removeTag(ENTITY_DATA_KEY); + tag.removeTag("entity"); + } + } + + public static NBTTagCompound getEntityData(ItemStack stack) + { + if (stack.hasTagCompound()) + { + NBTTagCompound tag = stack.getTagCompound(); + return tag.getCompoundTag(ENTITY_DATA_KEY); + } + return null; + } + + public static Entity getEntity(ItemStack stack, World world) + { + if (world == null) + return null; + + String name = getEntityName(stack); + + NBTTagCompound e = getEntityData(stack); + Entity entity = EntityList.createEntityByIDFromName(new ResourceLocation(name), world); + if (entity != null) + entity.readFromNBT(e); + + return entity; + } + + public static String getEntityName(ItemStack stack) + { + if (stack.hasTagCompound()) + { + NBTTagCompound tag = stack.getTagCompound(); + return tag.getString("entity"); + } + return null; + } + + private int potionLevel(ItemStack stack, World world) + { + Entity e = getEntity(stack, world); + if(e == null) + return 1; + + int i = (int) (e.height * e.width); + if (i > 4) + i = 4; + + if (!CarryOnConfig.settings.heavyEntities) + i = 1; + + return i; + } +} diff --git a/src/main/java/tschipp/carryon/common/item/ItemTile.java b/src/main/java/tschipp/carryon/common/item/ItemTile.java index 1361f2a..d5143c9 100644 --- a/src/main/java/tschipp/carryon/common/item/ItemTile.java +++ b/src/main/java/tschipp/carryon/common/item/ItemTile.java @@ -136,6 +136,9 @@ public class ItemTile extends Item { if (entity instanceof EntityLivingBase) { + if(entity instanceof EntityPlayer && CarryOnConfig.settings.slownessInCreative ? false : ((EntityPlayer)entity).isCreative()) + return; + ((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 1, potionLevel(stack), false, false)); } } diff --git a/src/main/resources/assets/carryon/lang/en_US.lang b/src/main/resources/assets/carryon/lang/en_US.lang index 7bcdd2d..88ef6f8 100644 --- a/src/main/resources/assets/carryon/lang/en_US.lang +++ b/src/main/resources/assets/carryon/lang/en_US.lang @@ -1,5 +1,8 @@ carryon.category.settings=Settings carryon.category.forbiddentiles=Forbidden Tile Entities carryon.category.modeloverrides=Model Overrides (Advanced) +carryon.category.forbiddenentities=Forbidden Entities -carryon.general.modeloverrides.modeloverrides=Model Overrides \ No newline at end of file +carryon.general.modeloverrides.modeloverrides=Model Overrides +carryon.general.forbiddenentities.forbiddenentities=Entities that the Player cannot pick up +carryon.general.forbiddentiles.forbiddentiles=Blocks that the Player cannot pick up \ No newline at end of file