Ranged Collector Mechanics

This commit is contained in:
Ellpeck 2015-10-10 02:51:06 +02:00
parent 4118cd732a
commit e0e6bc8b4a
9 changed files with 492 additions and 17 deletions

View file

@ -0,0 +1,111 @@
/*
* This file ("BlockRangedCollector.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.blocks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.ActuallyAdditions;
import ellpeck.actuallyadditions.inventory.GuiHandler;
import ellpeck.actuallyadditions.tile.TileEntityRangedCollector;
import ellpeck.actuallyadditions.util.IActAddItemOrBlock;
import ellpeck.actuallyadditions.util.ModUtil;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
public class BlockRangedCollector extends BlockContainerBase implements IActAddItemOrBlock{
public BlockRangedCollector(){
super(Material.rock);
this.setHarvestLevel("pickaxe", 0);
this.setHardness(1.5F);
this.setResistance(10.0F);
this.setStepSound(soundTypeStone);
}
@Override
public TileEntity createNewTileEntity(World world, int par2){
return new TileEntityRangedCollector();
}
@Override
public IIcon getIcon(int side, int meta){
return this.blockIcon;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){
if(!world.isRemote){
TileEntityRangedCollector breaker = (TileEntityRangedCollector)world.getTileEntity(x, y, z);
if(breaker != null){
player.openGui(ActuallyAdditions.instance, GuiHandler.GuiTypes.RANGED_COLLECTOR.ordinal(), world, x, y, z);
}
return true;
}
return true;
}
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconReg){
this.blockIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getName());
}
@Override
public String getName(){
return "blockRangedCollector";
}
@Override
public void breakBlock(World world, int x, int y, int z, Block block, int par6){
if(!world.isRemote){
TileEntity aTile = world.getTileEntity(x, y, z);
if(aTile instanceof TileEntityRangedCollector){
TileEntityRangedCollector tile = (TileEntityRangedCollector)aTile;
for(int i = 0; i < TileEntityRangedCollector.WHITELIST_START; i++){
this.dropSlotFromInventory(i, tile, world, x, y, z);
}
}
}
super.breakBlock(world, x, y, z, block, par6);
}
public static class TheItemBlock extends ItemBlock{
public TheItemBlock(Block block){
super(block);
this.setHasSubtypes(false);
this.setMaxDamage(0);
}
@Override
public String getUnlocalizedName(ItemStack stack){
return this.getUnlocalizedName();
}
@Override
public int getMetadata(int damage){
return damage;
}
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.epic;
}
}
}

View file

@ -95,10 +95,14 @@ public class InitBlocks{
public static Block blockLeafGenerator;
public static Block blockDirectionalBreaker;
public static Block blockRangedCollector;
public static void init(){
ModUtil.LOGGER.info("Initializing Blocks...");
blockRangedCollector = new BlockRangedCollector();
BlockUtil.register(blockRangedCollector);
blockDirectionalBreaker = new BlockDirectionalBreaker();
BlockUtil.register(blockDirectionalBreaker);

View file

@ -0,0 +1,116 @@
/*
* This file ("ContainerRangedCollector.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.inventory;
import ellpeck.actuallyadditions.inventory.slot.SlotFilter;
import ellpeck.actuallyadditions.tile.TileEntityBase;
import ellpeck.actuallyadditions.tile.TileEntityRangedCollector;
import invtweaks.api.container.InventoryContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
@InventoryContainer
public class ContainerRangedCollector extends Container{
private TileEntityRangedCollector collector;
public ContainerRangedCollector(InventoryPlayer inventory, TileEntityBase tile){
this.collector = (TileEntityRangedCollector)tile;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 3; j++){
this.addSlotToContainer(new Slot(this.collector, j+i*3, 96+j*18, 24+i*18));
}
}
for(int i = 0; i < 4; i++){
for(int j = 0; j < 3; j++){
this.addSlotToContainer(new SlotFilter(this.collector, 6+j+i*3, 20+j*18, 6+i*18));
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 9; j++){
this.addSlotToContainer(new Slot(inventory, j+i*9+9, 8+j*18, 90+i*18));
}
}
for(int i = 0; i < 9; i++){
this.addSlotToContainer(new Slot(inventory, i, 8+i*18, 148));
}
}
@Override
public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer player){
if(par1 >= 0 && par1 < this.inventorySlots.size() && this.getSlot(par1) instanceof SlotFilter){
//Calls the Filter's SlotClick function
return ((SlotFilter)getSlot(par1)).slotClick(player, par2);
}
else{
return super.slotClick(par1, par2, par3, player);
}
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot){
final int inventoryStart = 18;
final int inventoryEnd = inventoryStart+26;
final int hotbarStart = inventoryEnd+1;
final int hotbarEnd = hotbarStart+8;
Slot theSlot = (Slot)this.inventorySlots.get(slot);
if(theSlot != null && theSlot.getHasStack()){
ItemStack newStack = theSlot.getStack();
ItemStack currentStack = newStack.copy();
//Other Slots in Inventory excluded
if(slot >= inventoryStart){
//Shift from Inventory
if(!this.mergeItemStack(newStack, 0, TileEntityRangedCollector.WHITELIST_START, false)){
//
if(slot >= inventoryStart && slot <= inventoryEnd){
if(!this.mergeItemStack(newStack, hotbarStart, hotbarEnd+1, false)){
return null;
}
}
else if(slot >= inventoryEnd+1 && slot < hotbarEnd+1 && !this.mergeItemStack(newStack, inventoryStart, inventoryEnd+1, false)){
return null;
}
}
}
else if(!this.mergeItemStack(newStack, inventoryStart, hotbarEnd+1, false)){
return null;
}
if(newStack.stackSize == 0){
theSlot.putStack(null);
}
else{
theSlot.onSlotChanged();
}
if(newStack.stackSize == currentStack.stackSize){
return null;
}
theSlot.onPickupFromSlot(player, newStack);
return currentStack;
}
return null;
}
@Override
public boolean canInteractWith(EntityPlayer player){
return this.collector.isUseableByPlayer(player);
}
}

View file

@ -84,6 +84,8 @@ public class GuiHandler implements IGuiHandler{
return new ContainerSmileyCloud();
case DIRECTIONAL_BREAKER:
return new ContainerDirectionalBreaker(entityPlayer.inventory, tile);
case RANGED_COLLECTOR:
return new ContainerRangedCollector(entityPlayer.inventory, tile);
default:
return null;
}
@ -148,6 +150,8 @@ public class GuiHandler implements IGuiHandler{
return new GuiBooklet(null);
case DIRECTIONAL_BREAKER:
return new GuiDirectionalBreaker(entityPlayer.inventory, tile);
case RANGED_COLLECTOR:
return new GuiRangedCollector(entityPlayer.inventory, tile, x, y, z, world);
default:
return null;
}
@ -179,7 +183,8 @@ public class GuiHandler implements IGuiHandler{
ORE_MAGNET,
CLOUD,
BOOK(false),
DIRECTIONAL_BREAKER;
DIRECTIONAL_BREAKER,
RANGED_COLLECTOR;
public boolean checkTileEntity;

View file

@ -0,0 +1,99 @@
/*
* This file ("GuiRangedCollector.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.inventory.gui;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.inventory.ContainerRangedCollector;
import ellpeck.actuallyadditions.network.PacketHandler;
import ellpeck.actuallyadditions.network.gui.PacketGuiButton;
import ellpeck.actuallyadditions.tile.TileEntityBase;
import ellpeck.actuallyadditions.tile.TileEntityRangedCollector;
import ellpeck.actuallyadditions.util.AssetUtil;
import ellpeck.actuallyadditions.util.ModUtil;
import ellpeck.actuallyadditions.util.StringUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;
import java.util.Collections;
@SideOnly(Side.CLIENT)
public class GuiRangedCollector extends GuiContainer{
private static final ResourceLocation resLoc = AssetUtil.getGuiLocation("guiRangedCollector");
private TileEntityRangedCollector collector;
private GuiInputter.SmallerButton whitelistButton;
private int x;
private int y;
private int z;
private World world;
public GuiRangedCollector(InventoryPlayer inventory, TileEntityBase tile, int x, int y, int z, World world){
super(new ContainerRangedCollector(inventory, tile));
this.collector = (TileEntityRangedCollector)tile;
this.xSize = 176;
this.ySize = 86+86;
this.x = x;
this.y = y;
this.z = z;
this.world = world;
}
@SuppressWarnings("unchecked")
@Override
public void initGui(){
super.initGui();
this.whitelistButton = new GuiInputter.SmallerButton(0, guiLeft+3, guiTop+16, "");
this.buttonList.add(this.whitelistButton);
}
@Override
public void actionPerformed(GuiButton button){
PacketHandler.theNetwork.sendToServer(new PacketGuiButton(x, y, z, world, button.id, Minecraft.getMinecraft().thePlayer));
}
@Override
@SuppressWarnings("unchecked")
public void drawScreen(int x, int y, float f){
super.drawScreen(x, y, f);
this.whitelistButton.displayString = this.collector.isWhitelist ? "O" : "X";
String text1 = this.collector.isWhitelist ? StringUtil.localize("info."+ModUtil.MOD_ID_LOWER+".gui.whitelist") : StringUtil.localize("info."+ModUtil.MOD_ID_LOWER+".gui.blacklist");
if(x >= guiLeft+3 && y >= guiTop+16 && x <= guiLeft+18 && y <= guiTop+31){
this.func_146283_a(Collections.singletonList(text1), x, y);
}
}
@Override
public void drawGuiContainerForegroundLayer(int x, int y){
AssetUtil.displayNameString(this.fontRendererObj, xSize, -10, this.collector.getInventoryName());
}
@Override
public void drawGuiContainerBackgroundLayer(float f, int x, int y){
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(AssetUtil.GUI_INVENTORY_LOCATION);
this.drawTexturedModalRect(this.guiLeft, this.guiTop+86, 0, 0, 176, 86);
this.mc.getTextureManager().bindTexture(resLoc);
this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, 176, 86);
}
}

View file

@ -54,4 +54,13 @@ public class SlotFilter extends Slot{
super.putStack(theStack);
}
@Override
public boolean isItemValid(ItemStack stack){
return false;
}
@Override
public boolean canTakeStack(EntityPlayer player){
return false;
}
}

View file

@ -61,6 +61,7 @@ public class TileEntityBase extends TileEntity{
GameRegistry.registerTileEntity(TileEntitySmileyCloud.class, ModUtil.MOD_ID_LOWER+":tileEntityCloud");
GameRegistry.registerTileEntity(TileEntityLeafGenerator.class, ModUtil.MOD_ID_LOWER+":tileEntityLeafGenerator");
GameRegistry.registerTileEntity(TileEntityDirectionalBreaker.class, ModUtil.MOD_ID_LOWER+":tileEntityDirectionalBreaker");
GameRegistry.registerTileEntity(TileEntityRangedCollector.class, ModUtil.MOD_ID_LOWER+":tileEntityRangedCollector");
}
@Override

View file

@ -0,0 +1,124 @@
/*
* This file ("TileEntityRangedCollector.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.tile;
import ellpeck.actuallyadditions.network.gui.IButtonReactor;
import ellpeck.actuallyadditions.network.sync.IPacketSyncerToClient;
import ellpeck.actuallyadditions.network.sync.PacketSyncerToClient;
import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import java.util.ArrayList;
public class TileEntityRangedCollector extends TileEntityInventoryBase implements IButtonReactor, IPacketSyncerToClient{
public static final int WHITELIST_START = 6;
public boolean isWhitelist = true;
private boolean lastWhitelist;
public TileEntityRangedCollector(){
super(18, "rangedCollector");
}
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
if(!worldObj.isRemote){
if(!worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)){
int range = 8;
ArrayList<EntityItem> items = (ArrayList<EntityItem>)this.worldObj.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(this.xCoord-range, this.yCoord-range, this.zCoord-range, this.xCoord+range, this.yCoord+range, this.zCoord+range));
if(!items.isEmpty()){
for(EntityItem item : items){
if(item.getEntityItem() != null){
ItemStack toAdd = item.getEntityItem().copy();
if(this.checkFilter(toAdd)){
ArrayList<ItemStack> checkList = new ArrayList<ItemStack>();
checkList.add(toAdd);
if(WorldUtil.addToInventory(this.slots, 0, WHITELIST_START, checkList, false)){
WorldUtil.addToInventory(this.slots, 0, WHITELIST_START, checkList, true);
item.setDead();
}
}
}
}
}
}
if(this.isWhitelist != this.lastWhitelist){
this.lastWhitelist = this.isWhitelist;
this.sendUpdate();
}
}
}
private boolean checkFilter(ItemStack stack){
int slotStop = WHITELIST_START+12;
for(int i = WHITELIST_START; i < slotStop; i++){
if(this.slots[i] != null && this.slots[i].isItemEqual(stack)){
return this.isWhitelist;
}
}
return !this.isWhitelist;
}
@Override
public void readFromNBT(NBTTagCompound compound){
super.readFromNBT(compound);
this.isWhitelist = compound.getBoolean("Whitelist");
}
@Override
public void writeToNBT(NBTTagCompound compound){
super.writeToNBT(compound);
compound.setBoolean("Whitelist", this.isWhitelist);
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return false;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side){
return this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
return slot < WHITELIST_START;
}
@Override
public void onButtonPressed(int buttonID, EntityPlayer player){
this.isWhitelist = !this.isWhitelist;
}
@Override
public int[] getValues(){
return new int[]{this.isWhitelist ? 1 : 0};
}
@Override
public void setValues(int[] values){
this.isWhitelist = values[0] == 1;
}
@Override
public void sendUpdate(){
PacketSyncerToClient.sendPacket(this);
}
}

View file

@ -251,6 +251,10 @@ public class WorldUtil{
return blocks;
}
public static boolean addToInventory(ItemStack[] slots, ArrayList<ItemStack> stacks, boolean actuallyDo){
return addToInventory(slots, 0, slots.length, stacks, actuallyDo);
}
/**
* Add an ArrayList of ItemStacks to an Array of slots
*
@ -259,29 +263,31 @@ public class WorldUtil{
* @param actuallyDo Do it or just test if it works?
* @return Does it work?
*/
public static boolean addToInventory(ItemStack[] slots, ArrayList<ItemStack> stacks, boolean actuallyDo){
ItemStack[] testSlots = new ItemStack[slots.length];
for(int i = 0; i < testSlots.length; i++){
if(slots[i] != null){
testSlots[i] = slots[i].copy();
public static boolean addToInventory(ItemStack[] slots, int start, int end, ArrayList<ItemStack> stacks, boolean actuallyDo){
ItemStack[] theSlots;
if(actuallyDo){
theSlots = slots;
}
else{
//Create "Test Slots" to put the items into to try if it works out in the end
theSlots = new ItemStack[slots.length];
for(int i = 0; i < theSlots.length; i++){
if(slots[i] != null){
theSlots[i] = slots[i].copy();
}
}
}
int working = 0;
for(ItemStack stackToPutIn : stacks){
for(int i = 0; i < testSlots.length; i++){
if(stackToPutIn != null && (testSlots[i] == null || (testSlots[i].isItemEqual(stackToPutIn) && testSlots[i].getMaxStackSize() >= testSlots[i].stackSize+stackToPutIn.stackSize))){
if(testSlots[i] == null){
if(actuallyDo){
slots[i] = stackToPutIn.copy();
}
testSlots[i] = stackToPutIn.copy();
for(int i = start; i < end; i++){
if(stackToPutIn != null && (theSlots[i] == null || (theSlots[i].isItemEqual(stackToPutIn) && theSlots[i].getMaxStackSize() >= theSlots[i].stackSize+stackToPutIn.stackSize))){
if(theSlots[i] == null){
theSlots[i] = stackToPutIn.copy();
}
else{
if(actuallyDo){
slots[i].stackSize += stackToPutIn.stackSize;
}
testSlots[i].stackSize += stackToPutIn.stackSize;
theSlots[i].stackSize += stackToPutIn.stackSize;
}
working++;