From 7298c13454af4a3e87e73fee488e9b8327df2242 Mon Sep 17 00:00:00 2001 From: Lizbeth Rika Date: Sun, 15 Feb 2015 17:27:14 +0000 Subject: [PATCH] Basic requesting code. --- .../botania/api/corporea/CorporeaHelper.java | 74 ++++++++++++------- .../render/tile/RenderTileCorporeaIndex.java | 2 +- .../tile/corporea/TileCorporeaIndex.java | 72 +++++++++++++++++- .../common/core/proxy/CommonProxy.java | 4 +- 4 files changed, 123 insertions(+), 29 deletions(-) diff --git a/src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java b/src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java index 3a8161969..a299c004e 100644 --- a/src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java +++ b/src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java @@ -127,47 +127,60 @@ public final class CorporeaHelper { } /** - * Requests an ItemStack of the type passed in from the network, or tries to, checking NBT or not. - * This will remove the items from the adequate inventories unless the "doit" parameter is false. - * Returns a new ItemStack of the item acquired or null if none was found. + * Bridge for requestItem() using an ItemStack. */ - public static ItemStack requestItem(ItemStack stack, ICorporeaSpark spark, boolean checkNBT, boolean doit) { - Map inventories = getInventoriesWithItemInNetwork(stack, spark, checkNBT); - int count = getCountInNetwork(stack, inventories, checkNBT); - int size = Math.min(count, stack.stackSize); - if(size == 0) - return null; + public static List requestItem(ItemStack stack, ICorporeaSpark spark, boolean checkNBT, boolean doit) { + return requestItem(stack, stack.stackSize, spark, checkNBT, doit); + } + + /** + * Bridge for requestItem() using a String and an item count. + */ + public static List requestItem(String name, int count, ICorporeaSpark spark, boolean doit) { + return requestItem(name, count, spark, false, doit); + } - ItemStack retStack = stack.copy(); - retStack.stackSize = size; - if(!doit) - return retStack; - - int remove = size; + /** + * Requests list of ItemStacks of the type passed in from the network, or tries to, checking NBT or not. + * This will remove the items from the adequate inventories unless the "doit" parameter is false. + * Returns a new list of ItemStacks of the items acquired or an empty list if none was found. + *

+ * The "matcher" parameter has to be an ItemStack or a String, if the first it'll check if the + * two stacks are similar using the "checkNBT" parameter, else it'll check if the name of the item + * equals or matches (case a regex is passed in) the matcher string. + */ + public static List requestItem(Object matcher, int itemCount, ICorporeaSpark spark, boolean checkNBT, boolean doit) { + List inventories = getInventoriesOnNetwork(spark); + List stacks = new ArrayList(); + + int count = itemCount; checkInvs : { - for(IInventory inv : inventories.keySet()) { + for(IInventory inv : inventories) { for(int i = 0; i < inv.getSizeInventory(); i++) { if(!isValidSlot(inv, i)) continue; ItemStack stackAt = inv.getStackInSlot(i); - if(stacksMatch(stack, stackAt, checkNBT)) { - int rem = Math.min(stackAt.stackSize, remove); - stackAt.stackSize -= rem; - remove -= rem; + if(matcher instanceof ItemStack ? stacksMatch((ItemStack) matcher, stackAt, checkNBT) : matcher instanceof String ? stacksMatch(stackAt, (String) matcher) : false) { + int rem = Math.min(stackAt.stackSize, count); + stacks.add(stackAt.copy()); + if(doit) { + stackAt.stackSize -= rem; + if(stackAt.stackSize == 0) + inv.setInventorySlotContents(i, null); + } + count -= rem; - if(stackAt.stackSize == 0) - inv.setInventorySlotContents(i, null); - if(remove <= 0) + if(count <= 0) break checkInvs; } } } } - return retStack; + return stacks; } - + /** * Gets the spark attached to the block in the coords passed in. Note that the coords passed * in are for the block that the spark will be on, not the coords of the spark itself. @@ -198,6 +211,17 @@ public final class CorporeaHelper { public static boolean stacksMatch(ItemStack stack1, ItemStack stack2, boolean checkNBT) { return stack1 != null && stack2 != null && stack1.isItemEqual(stack2) && (!checkNBT || ItemStack.areItemStackTagsEqual(stack1, stack2)); } + + /** + * Gets if the name of a stack matches the string passed in. + */ + public static boolean stacksMatch(ItemStack stack, String s) { + if(stack == null) + return false; + + String name = stack.getDisplayName().toLowerCase().trim(); + return name.equals(s) || (name + "s").equals(s) || (name + "es").equals(s) || (name.endsWith("y") && (name.substring(0, name.length() - 1) + "ies").equals(s)); + } /** * Clears the cached networks, called once per tick, should not be called outside diff --git a/src/main/java/vazkii/botania/client/render/tile/RenderTileCorporeaIndex.java b/src/main/java/vazkii/botania/client/render/tile/RenderTileCorporeaIndex.java index 0a782f40e..dcaefff71 100644 --- a/src/main/java/vazkii/botania/client/render/tile/RenderTileCorporeaIndex.java +++ b/src/main/java/vazkii/botania/client/render/tile/RenderTileCorporeaIndex.java @@ -36,7 +36,7 @@ public class RenderTileCorporeaIndex extends TileEntitySpecialRenderer { GL11.glTranslated(x + 0.5, y, z + 0.5); Minecraft.getMinecraft().renderEngine.bindTexture(texture); - float translation = move ? (float) ((Math.cos((float) index.ticksWithCloseby / 10F) * 0.5 + 0.5) * 0.25) : 0F; + float translation = move ? (float) ((Math.cos((float) (index.ticksWithCloseby + (index.hasCloseby ? partticks : 0)) / 10F) * 0.5 + 0.5) * 0.25) : 0F; float rotation = move ? (index.ticks * 2 + partticks) : 0F; float scale = 0.6F; GL11.glScalef(scale, scale, scale); diff --git a/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java b/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java index f6e25a167..3f291ef2e 100644 --- a/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java +++ b/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java @@ -10,16 +10,32 @@ */ package vazkii.botania.common.block.tile.corporea; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Set; +import java.util.WeakHashMap; -import vazkii.botania.common.core.helper.MathHelper; +import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.world.World; +import net.minecraftforge.event.ServerChatEvent; +import vazkii.botania.api.corporea.CorporeaHelper; +import vazkii.botania.api.corporea.ICorporeaSpark; +import vazkii.botania.common.block.ModBlocks; +import vazkii.botania.common.core.helper.MathHelper; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class TileCorporeaIndex extends TileCorporeaBase { public static final double RADIUS = 2.5; + public static final InputHandler input = new InputHandler(); + public static final Set indexes = Collections.newSetFromMap(new WeakHashMap()); + public int ticks = 0; public int ticksWithCloseby = 0; public float closeby = 0F; @@ -36,7 +52,7 @@ public class TileCorporeaIndex extends TileCorporeaBase { List players = worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(x - RADIUS, y - RADIUS, z - RADIUS, x + RADIUS, y + RADIUS, z + RADIUS)); hasCloseby = false; for(EntityPlayer player : players) - if(MathHelper.pointDistanceSpace(x, y, z, player.posX, player.posY, player.posZ) < RADIUS) { + if(isInRangeOfIndex(player, this)) { hasCloseby = true; break; } @@ -49,6 +65,21 @@ public class TileCorporeaIndex extends TileCorporeaBase { closeby += step; } else if(closeby > 0F) closeby -= step; + + if(!isInvalid() && !indexes.contains(this)) + indexes.add(this); + } + + @Override + public void invalidate() { + super.invalidate(); + indexes.remove(this); + } + + @Override + public void onChunkUnload() { + super.onChunkUnload(); + indexes.remove(this); } @Override @@ -60,5 +91,42 @@ public class TileCorporeaIndex extends TileCorporeaBase { public String getInventoryName() { return null; } + + public static boolean isInRangeOfIndex(EntityPlayer player, TileCorporeaIndex index) { + return MathHelper.pointDistanceSpace(index.xCoord + 0.5, index.yCoord + 0.5, index.zCoord + 0.5, player.posX, player.posY, player.posZ) < RADIUS; + } + + public static final class InputHandler { + + @SubscribeEvent + public void onChatMessage(ServerChatEvent event) { + List nearbyIndexes = getNearbyIndexes(event.player); + if(!nearbyIndexes.isEmpty()) { + String msg = event.message.toLowerCase().trim(); + for(TileCorporeaIndex index : nearbyIndexes) { + ICorporeaSpark spark = index.getSpark(); + if(spark != null) { + List stacks = CorporeaHelper.requestItem(msg, 64, spark, true); + for(ItemStack stack : stacks) + if(stack != null) { + EntityItem item = new EntityItem(index.worldObj, index.xCoord + 0.5, index.yCoord + 1.5, index.zCoord + 0.5, stack); + index.worldObj.spawnEntityInWorld(item); + } + } + } + + event.setCanceled(true); + } + } + + public List getNearbyIndexes(EntityPlayer player) { + List indexList = new ArrayList(); + for(TileCorporeaIndex index : indexes) + if(isInRangeOfIndex(player, index) && index.worldObj.isRemote == player.worldObj.isRemote) + indexList.add(index); + return indexList; + } + + } } diff --git a/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java b/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java index 88b849f23..249702062 100644 --- a/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java +++ b/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java @@ -24,6 +24,7 @@ import vazkii.botania.common.Botania; import vazkii.botania.common.achievement.ModAchievements; import vazkii.botania.common.block.ModBlocks; import vazkii.botania.common.block.subtile.generating.SubTileNarslimmus; +import vazkii.botania.common.block.tile.corporea.TileCorporeaIndex; import vazkii.botania.common.brew.ModBrews; import vazkii.botania.common.brew.ModPotions; import vazkii.botania.common.core.command.CommandDownloadLatest; @@ -98,7 +99,8 @@ public class CommonProxy { MinecraftForge.EVENT_BUS.register(new SheddingHandler()); MinecraftForge.EVENT_BUS.register(new SpawnerChangingHandler()); MinecraftForge.EVENT_BUS.register(new SubTileNarslimmus.SpawnIntercepter()); - + MinecraftForge.EVENT_BUS.register(TileCorporeaIndex.input); + FMLCommonHandler.instance().bus().register(new CommonTickHandler()); FMLInterModComms.sendMessage("ProjectE", "interdictionblacklist", EntityManaBurst.class.getCanonicalName());