From 06f62473f0622efe6decc32b70516a7c5d3d3572 Mon Sep 17 00:00:00 2001 From: OnyxDarkKnight Date: Wed, 17 Sep 2014 19:10:05 +0300 Subject: 1.7.10 --- .../jewelrycraft/events/BucketHandler.java | 83 +++++++++++++++ .../jewelrycraft/events/EntityEventHandler.java | 118 +++++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 java/darkknight/jewelrycraft/events/BucketHandler.java create mode 100644 java/darkknight/jewelrycraft/events/EntityEventHandler.java (limited to 'java/darkknight/jewelrycraft/events') diff --git a/java/darkknight/jewelrycraft/events/BucketHandler.java b/java/darkknight/jewelrycraft/events/BucketHandler.java new file mode 100644 index 0000000..ec0b280 --- /dev/null +++ b/java/darkknight/jewelrycraft/events/BucketHandler.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public License + * 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ +package darkknight.jewelrycraft.events; + +import java.util.HashMap; +import java.util.Map; + +import net.minecraft.block.Block; +import net.minecraft.client.Minecraft; +import net.minecraft.init.Blocks; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.world.World; +import net.minecraftforge.event.entity.player.FillBucketEvent; +import cpw.mods.fml.common.eventhandler.Event.Result; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import darkknight.jewelrycraft.JewelrycraftMod; +import darkknight.jewelrycraft.block.BlockMoltenMetal; +import darkknight.jewelrycraft.util.JewelryNBT; + +public class BucketHandler +{ + + public static BucketHandler INSTANCE = new BucketHandler(); + public Map buckets = new HashMap(); + + private BucketHandler() + { + } + + @SubscribeEvent + public void onBucketFill(FillBucketEvent event) + { + + ItemStack result = fillCustomBucket(event.world, event.target); + + if (result == null) return; + + event.result = result; + event.setResult(Result.ALLOW); + } + + private ItemStack fillCustomBucket(World world, MovingObjectPosition pos) + { + + Block block = world.getBlock(pos.blockX, pos.blockY, pos.blockZ); + Item bucket = buckets.get(block); + + if (bucket != null && world.getBlock(pos.blockX, pos.blockY, pos.blockZ) != Blocks.air && world.getBlock(pos.blockX, pos.blockY, pos.blockZ) instanceof BlockMoltenMetal) + { + world.setBlockToAir(pos.blockX, pos.blockY, pos.blockZ); + ItemStack item = new ItemStack(bucket); + String ingotData = JewelrycraftMod.saveData.getString(pos.blockX + " " + pos.blockY + " " + pos.blockZ + " " + world.provider.dimensionId); + if (ingotData != null && ingotData != "") + { + String[] splitData = ingotData.split(":"); + if (splitData.length == 2) + { + int itemID, itemDamage; + try + { + itemID = Integer.parseInt(splitData[0]); + itemDamage = Integer.parseInt(splitData[1]); + + JewelryNBT.addMetal(item, new ItemStack(Item.getItemById(itemID), 1, itemDamage)); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + } + return item; + } + else return null; + } +} \ No newline at end of file diff --git a/java/darkknight/jewelrycraft/events/EntityEventHandler.java b/java/darkknight/jewelrycraft/events/EntityEventHandler.java new file mode 100644 index 0000000..ed7c3e7 --- /dev/null +++ b/java/darkknight/jewelrycraft/events/EntityEventHandler.java @@ -0,0 +1,118 @@ +package darkknight.jewelrycraft.events; + +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.event.entity.EntityJoinWorldEvent; +import net.minecraftforge.event.world.WorldEvent; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import darkknight.jewelrycraft.JewelrycraftMod; +import darkknight.jewelrycraft.item.ItemList; +import darkknight.jewelrycraft.network.PacketClearColorCache; +import darkknight.jewelrycraft.util.BlockUtils; +import darkknight.jewelrycraft.util.PlayerUtils; + +/** + * Code taken from OpenBlocks + */ + +public class EntityEventHandler +{ + public static final String OPENBLOCKS_PERSIST_TAG = "Jewelrycraft"; + public static final String GIVEN_GUIDE_TAG = "givenGuive"; + + @SubscribeEvent + @SideOnly(Side.SERVER) + public void onEntityJoinWorld(EntityJoinWorldEvent event) + { + if (event.entity instanceof EntityPlayerMP) JewelrycraftMod.netWrapper.sendTo(new PacketClearColorCache(), (EntityPlayerMP) event.entity); + + final Entity entity = event.entity; + + if (!event.world.isRemote && entity instanceof EntityPlayer) + { + EntityPlayer player = (EntityPlayer) entity; + NBTTagCompound persistTag = PlayerUtils.getModPlayerPersistTag(player, "Jewelrycraft"); + + boolean shouldGiveManual = ItemList.guide != null && !persistTag.getBoolean(GIVEN_GUIDE_TAG); + if (shouldGiveManual) + { + ItemStack manual = new ItemStack(ItemList.guide); + if (!player.inventory.addItemStackToInventory(manual)) + { + BlockUtils.dropItemStackInWorld(player.worldObj, player.posX, player.posY, player.posZ, manual); + } + persistTag.setBoolean(GIVEN_GUIDE_TAG, true); + } + } + } + + @SubscribeEvent + public void onWorldLoad(WorldEvent.Load event) + { + if (!event.world.isRemote) + { + new File(JewelrycraftMod.dir + File.separator + "Jewelrycraft").mkdirs(); + JewelrycraftMod.liquidsConf = new File(JewelrycraftMod.dir + File.separator + "Jewelrycraft", "JLP" + event.world.getWorldInfo().getWorldName() + ".cfg"); + try + { + if (!JewelrycraftMod.liquidsConf.exists()) JewelrycraftMod.liquidsConf.createNewFile(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) + { + try + { + if (JewelrycraftMod.liquidsConf.exists()) JewelrycraftMod.saveData = CompressedStreamTools.readCompressed(new FileInputStream(JewelrycraftMod.liquidsConf)); + } + catch (EOFException e) + { + e.printStackTrace(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + } + + @SubscribeEvent + public void onWorldSave(WorldEvent.Save event) + { + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) + { + try + { + if (JewelrycraftMod.liquidsConf.exists()) CompressedStreamTools.writeCompressed(JewelrycraftMod.saveData, new FileOutputStream(JewelrycraftMod.liquidsConf)); + } + catch (EOFException e) + { + e.printStackTrace(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } +} \ No newline at end of file -- cgit v1.2.3