Basic requesting code.

This commit is contained in:
Lizbeth Rika committed 2015-02-15 17:27:14 +00:00
1 parent 0c425a2699
commit 7298c13454
4 files changed
+123 -29

No files matched your search

@@ -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<IInventory, Integer> 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<ItemStack> 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<ItemStack> 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.
* <br><br>
* 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<ItemStack> requestItem(Object matcher, int itemCount, ICorporeaSpark spark, boolean checkNBT, boolean doit) {
List<IInventory> inventories = getInventoriesOnNetwork(spark);
List<ItemStack> 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
@@ -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);
@@ -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<TileCorporeaIndex> 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<EntityPlayer> 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<TileCorporeaIndex> 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<ItemStack> 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<TileCorporeaIndex> getNearbyIndexes(EntityPlayer player) {
List<TileCorporeaIndex> indexList = new ArrayList();
for(TileCorporeaIndex index : indexes)
if(isInRangeOfIndex(player, index) && index.worldObj.isRemote == player.worldObj.isRemote)
indexList.add(index);
return indexList;
}
}
}
@@ -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());