From 53f6ce0a242ac529b58acb1ca74ee17c1d0d095a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 16 Nov 2016 18:51:23 +0100 Subject: [PATCH] Add Laser Relay priorities --- .../mod/blocks/BlockLaserRelay.java | 85 ++++++++++++++++++- .../mod/tile/TileEntityItemViewer.java | 12 +-- .../mod/tile/TileEntityLaserRelayItem.java | 22 +++++ .../TileEntityLaserRelayItemWhitelist.java | 5 ++ .../assets/actuallyadditions/lang/en_US.lang | 2 +- 5 files changed, 115 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java index cf0127d99..d697f8399 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java @@ -10,27 +10,40 @@ package de.ellpeck.actuallyadditions.mod.blocks; +import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; +import de.ellpeck.actuallyadditions.api.laser.Network; import de.ellpeck.actuallyadditions.mod.ActuallyAdditions; import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase; import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler; import de.ellpeck.actuallyadditions.mod.tile.*; +import de.ellpeck.actuallyadditions.mod.util.StackUtil; +import de.ellpeck.actuallyadditions.mod.util.StringUtil; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyInteger; import net.minecraft.block.state.IBlockState; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ScaledResolution; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.EnumRarity; +import net.minecraft.item.ItemCompass; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.util.text.TextFormatting; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import net.minecraftforge.fml.common.eventhandler.Event; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -public class BlockLaserRelay extends BlockContainerBase{ +public class BlockLaserRelay extends BlockContainerBase implements IHudDisplay{ //This took way too much fiddling around. I'm not good with numbers. private static final float F = 1/16F; @@ -52,6 +65,27 @@ public class BlockLaserRelay extends BlockContainerBase{ this.setSoundType(SoundType.STONE); this.type = type; + + if(this.type.ordinal() == 0){ + MinecraftForge.EVENT_BUS.register(this); + } + } + + @SubscribeEvent + public void onBlockRightClick(PlayerInteractEvent.RightClickBlock event){ + EntityPlayer player = event.getEntityPlayer(); + World world = event.getWorld(); + ItemStack stack = event.getItemStack(); + BlockPos pos = event.getPos(); + + if(player != null && world != null && StackUtil.isValid(stack) && pos != null){ + IBlockState state = event.getWorld().getBlockState(pos); + if(state != null && state.getBlock() instanceof BlockLaserRelay){ + if(stack.getItem() instanceof ItemCompass && player.isSneaking()){ + event.setUseBlock(Event.Result.ALLOW); + } + } + } } @Override @@ -100,9 +134,29 @@ public class BlockLaserRelay extends BlockContainerBase{ @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack stack, EnumFacing par6, float par7, float par8, float par9){ - if(player.isSneaking()){ - TileEntityLaserRelay relay = (TileEntityLaserRelay)world.getTileEntity(pos); - if(relay instanceof TileEntityLaserRelayItemWhitelist){ + TileEntityLaserRelay tile = (TileEntityLaserRelay)world.getTileEntity(pos); + if(tile instanceof TileEntityLaserRelayItem){ + TileEntityLaserRelayItem relay = (TileEntityLaserRelayItem)tile; + + if(StackUtil.isValid(stack) && stack.getItem() instanceof ItemCompass){ + if(player.isSneaking()){ + relay.priority--; + } + else{ + relay.priority++; + } + + Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(relay.getPos(), relay.getWorld()); + if(network != null){ + network.changeAmount++; + } + + relay.markDirty(); + relay.sendUpdate(); + + return true; + } + else if(relay instanceof TileEntityLaserRelayItemWhitelist && player.isSneaking()){ if(!world.isRemote){ player.openGui(ActuallyAdditions.instance, GuiHandler.GuiTypes.LASER_RELAY_ITEM_WHITELIST.ordinal(), world, pos.getX(), pos.getY(), pos.getZ()); } @@ -130,6 +184,29 @@ public class BlockLaserRelay extends BlockContainerBase{ } } + @Override + public void displayHud(Minecraft minecraft, EntityPlayer player, ItemStack stack, RayTraceResult posHit, ScaledResolution resolution){ + if(posHit != null && posHit.getBlockPos() != null && minecraft.theWorld != null){ + TileEntity tile = minecraft.theWorld.getTileEntity(posHit.getBlockPos()); + if(tile instanceof TileEntityLaserRelayItem){ + TileEntityLaserRelayItem relay = (TileEntityLaserRelayItem)tile; + + String strg = "Priority: "+TextFormatting.DARK_RED+relay.getPriority()+TextFormatting.RESET; + minecraft.fontRendererObj.drawStringWithShadow(strg, resolution.getScaledWidth()/2+5, resolution.getScaledHeight()/2+5, StringUtil.DECIMAL_COLOR_WHITE); + + String expl; + if(stack != null && stack.getItem() instanceof ItemCompass){ + expl = TextFormatting.GREEN+"Right-Click to increase! \nSneak-Right-Click to decrease!"; + } + else{ + expl = TextFormatting.GRAY.toString()+TextFormatting.ITALIC+"Hold a Compass to modify!"; + } + + StringUtil.drawSplitString(minecraft.fontRendererObj, expl, resolution.getScaledWidth()/2+5, resolution.getScaledHeight()/2+15, Integer.MAX_VALUE, StringUtil.DECIMAL_COLOR_WHITE, true); + } + } + } + public enum Type{ ENERGY_BASIC, ENERGY_ADVANCED, diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java index 6f3709259..3cb632ba3 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java @@ -276,17 +276,17 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ @Override public int compareTo(GenericItemHandlerInfo other){ - boolean thisWhitelist = this.relayInQuestion instanceof TileEntityLaserRelayItemWhitelist; - boolean otherWhitelist = other.relayInQuestion instanceof TileEntityLaserRelayItemWhitelist; + int thisPrio = this.relayInQuestion.getPriority(); + int otherPrio = other.relayInQuestion.getPriority(); - if(!thisWhitelist && otherWhitelist){ - return 1; + if(thisPrio == otherPrio){ + return 0; } - else if(thisWhitelist && !otherWhitelist){ + else if(thisPrio > otherPrio){ return -1; } else{ - return 0; + return 1; } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java index 1ec1a424c..34ce112b4 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java @@ -17,6 +17,7 @@ import de.ellpeck.actuallyadditions.api.laser.Network; import de.ellpeck.actuallyadditions.mod.tile.TileEntityItemViewer.GenericItemHandlerInfo; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; @@ -31,6 +32,7 @@ import java.util.concurrent.ConcurrentHashMap; public class TileEntityLaserRelayItem extends TileEntityLaserRelay{ + public int priority; public final Map handlersAround = new ConcurrentHashMap(); public TileEntityLaserRelayItem(String name){ @@ -41,6 +43,10 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{ this("laserRelayItem"); } + public int getPriority(){ + return this.priority; + } + public boolean isWhitelisted(ItemStack stack, boolean output){ return true; } @@ -109,4 +115,20 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{ } } } + + @Override + public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + super.writeSyncableNBT(compound, type); + if(type != NBTType.SAVE_BLOCK){ + compound.setInteger("Priority", this.priority); + } + } + + @Override + public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + super.readSyncableNBT(compound, type); + if(type != NBTType.SAVE_BLOCK){ + this.priority = compound.getInteger("Priority"); + } + } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java index 06b261c4d..16ad52c74 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java @@ -164,6 +164,11 @@ public class TileEntityLaserRelayItemWhitelist extends TileEntityLaserRelayItem }.setTile(this); } + @Override + public int getPriority(){ + return super.getPriority()+10; + } + @Override public boolean isWhitelisted(ItemStack stack, boolean output){ return output ? this.rightFilter.check(stack, this.slots) : this.leftFilter.check(stack, this.slots); diff --git a/src/main/resources/assets/actuallyadditions/lang/en_US.lang b/src/main/resources/assets/actuallyadditions/lang/en_US.lang index 58c3e45cc..5eac2cc4c 100644 --- a/src/main/resources/assets/actuallyadditions/lang/en_US.lang +++ b/src/main/resources/assets/actuallyadditions/lang/en_US.lang @@ -994,7 +994,7 @@ booklet.actuallyadditions.chapter.itemStorage.text.5=But how to make a system th booklet.actuallyadditions.chapter.itemStorage.text.6=Here, you can see the use of an Advanced Item Laser Relay. They can have a whitelist/blacklist configuration for both connections going into them (inbound, the left side in this case) and out of them (outbound, the right side in this case). booklet.actuallyadditions.chapter.itemStorage.text.7=You can also attach these advanced relays to Item Interfaces, which will mean that they will only have access to certain items from the system. Here, outbound means out of the system, and inbound means into the system. booklet.actuallyadditions.chapter.itemStorage.text.8=What you can do is take this to the next level and build a fully-fledged storage system with multiple chests. Pulling out of or inputting into an Item Interface here will pull out of the entire system. -booklet.actuallyadditions.chapter.itemStorage.text.9=Addendum: Advanced Item Laser Relays will always take priority, meaning inventories connected to them will be input into and pulled out of first. This can be useful if you want to make a storage system with a last resort-type chest. Also, this system isn't only limited to chests, and there can be way more than just two Item Interfaces! Gigantic storage system ahoy! +booklet.actuallyadditions.chapter.itemStorage.text.9=To make this system work in more complex situations, the Priority of a Laser Relay can be changed by holding a Compass and right-clicking on it. A higher priority means that the Laser Relay will first receive items and first have them extracted from it. You could say that it is first in line for being dealt with by an Item Interface. By defualt, the Advanced Item Laser Relay has a priority of 10. Priorities can be pretty much infinite and even negative! booklet.actuallyadditions.chapter.banners.name=Additional Banners booklet.actuallyadditions.chapter.banners.text.1=For special items in Actually Additions, there is also special Banner patterns. All of these just require the item next to the banner in the crafting grid with, optionally, a color. You can also combine them with a Shield like normal. The items that have a banner pattern are: The Actually Additions Manual The Phantom Connector The Leaf Blower (not the advanced version) The Drill (only the white one works due to the way banners work)