From 3e10688a96773f41c1849968f8fef665d97b9691 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 20 Apr 2020 13:12:26 +0200 Subject: [PATCH] finished the pipe frame --- .../java/de/ellpeck/prettypipes/Registry.java | 3 +- .../java/de/ellpeck/prettypipes/Utility.java | 15 +++ .../prettypipes/entities/PipeFrameEntity.java | 112 ++++++++++++++++-- .../entities/PipeFrameRenderer.java | 6 +- .../ellpeck/prettypipes/items/ModuleItem.java | 9 +- .../prettypipes/items/PipeFrameItem.java | 17 ++- .../assets/prettypipes/lang/en_us.json | 2 + .../prettypipes/models/item/pipe_frame.json | 6 + .../prettypipes/textures/item/pipe_frame.png | Bin 0 -> 365 bytes .../data/prettypipes/recipes/pipe_frame.json | 17 +++ 10 files changed, 162 insertions(+), 25 deletions(-) create mode 100644 src/main/resources/assets/prettypipes/models/item/pipe_frame.json create mode 100644 src/main/resources/assets/prettypipes/textures/item/pipe_frame.png create mode 100644 src/main/resources/data/prettypipes/recipes/pipe_frame.json diff --git a/src/main/java/de/ellpeck/prettypipes/Registry.java b/src/main/java/de/ellpeck/prettypipes/Registry.java index 213de31..efdd864 100644 --- a/src/main/java/de/ellpeck/prettypipes/Registry.java +++ b/src/main/java/de/ellpeck/prettypipes/Registry.java @@ -70,6 +70,7 @@ public final class Registry { public static Capability pipeNetworkCapability; public static Item wrenchItem; + public static Item pipeFrameItem; public static Block pipeBlock; public static TileEntityType pipeTileEntity; @@ -95,7 +96,7 @@ public final class Registry { registry.registerAll( wrenchItem = new WrenchItem().setRegistryName("wrench"), new Item(new Item.Properties().group(GROUP)).setRegistryName("blank_module"), - new PipeFrameItem().setRegistryName("pipe_frame") + pipeFrameItem = new PipeFrameItem().setRegistryName("pipe_frame") ); registry.registerAll(createTieredModule("extraction_module", ExtractionModuleItem::new)); registry.registerAll(createTieredModule("filter_module", FilterModuleItem::new)); diff --git a/src/main/java/de/ellpeck/prettypipes/Utility.java b/src/main/java/de/ellpeck/prettypipes/Utility.java index c0d3967..8ca4814 100644 --- a/src/main/java/de/ellpeck/prettypipes/Utility.java +++ b/src/main/java/de/ellpeck/prettypipes/Utility.java @@ -1,13 +1,18 @@ package de.ellpeck.prettypipes; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.resources.I18n; import net.minecraft.inventory.InventoryHelper; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.*; import net.minecraft.world.World; import net.minecraftforge.items.IItemHandler; +import java.util.List; + public final class Utility { public static T getTileEntity(Class type, World world, BlockPos pos) { @@ -28,4 +33,14 @@ public final class Utility { BlockPos diff = pos.subtract(other); return Direction.getFacingFromVector(diff.getX(), diff.getY(), diff.getZ()); } + + public static void addTooltip(String name, List tooltip) { + if (Screen.hasShiftDown()) { + String[] content = I18n.format("info." + PrettyPipes.ID + "." + name).split("\n"); + for (String s : content) + tooltip.add(new StringTextComponent(s).setStyle(new Style().setColor(TextFormatting.GRAY))); + } else { + tooltip.add(new TranslationTextComponent("info." + PrettyPipes.ID + ".shift").setStyle(new Style().setColor(TextFormatting.DARK_GRAY))); + } + } } diff --git a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java index 88b6e8b..d118cd6 100644 --- a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java @@ -1,20 +1,33 @@ package de.ellpeck.prettypipes.entities; +import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.PipeNetwork; +import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeTileEntity; +import net.minecraft.block.BlockState; +import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.item.ItemFrameEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.FilledMapItem; import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.network.IPacket; import net.minecraft.network.datasync.DataParameter; import net.minecraft.network.datasync.DataSerializers; import net.minecraft.network.datasync.EntityDataManager; +import net.minecraft.util.DamageSource; import net.minecraft.util.Direction; +import net.minecraft.util.Hand; +import net.minecraft.util.SoundEvents; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.GameRules; import net.minecraft.world.World; +import net.minecraft.world.storage.MapData; import net.minecraftforge.fml.network.NetworkHooks; +import javax.annotation.Nullable; import java.util.List; public class PipeFrameEntity extends ItemFrameEntity { @@ -34,7 +47,7 @@ public class PipeFrameEntity extends ItemFrameEntity { @Override protected void registerData() { super.registerData(); - this.dataManager.register(AMOUNT, 0); + this.dataManager.register(AMOUNT, -1); } @Override @@ -45,20 +58,101 @@ public class PipeFrameEntity extends ItemFrameEntity { if (this.ticksExisted % 40 != 0) return; PipeNetwork network = PipeNetwork.get(this.world); - BlockPos hangingPos = this.getHangingPosition().offset(this.getHorizontalFacing().getOpposite()); - BlockPos node = network.getNodeFromPipe(hangingPos); - if (node == null) - return; - ItemStack stack = this.getDisplayedItem(); - List items = network.getOrderedNetworkItems(node); - int amount = items.stream().mapToInt(i -> i.getItemAmount(stack)).sum(); - this.dataManager.set(AMOUNT, amount); + BlockPos attached = getAttachedPipe(this.world, this.hangingPosition, this.facingDirection); + if (attached != null) { + BlockPos node = network.getNodeFromPipe(attached); + if (node != null) { + ItemStack stack = this.getDisplayedItem(); + if (!stack.isEmpty()) { + List items = network.getOrderedNetworkItems(node); + int amount = items.stream().mapToInt(i -> i.getItemAmount(stack)).sum(); + this.dataManager.set(AMOUNT, amount); + return; + } + } + } + this.dataManager.set(AMOUNT, -1); + } + + @Override + public boolean onValidSurface() { + return super.onValidSurface() && canPlace(this.world, this.hangingPosition, this.facingDirection); + } + + private static BlockPos getAttachedPipe(World world, BlockPos pos, Direction direction) { + for (int i = 1; i <= 2; i++) { + BlockPos offset = pos.offset(direction.getOpposite(), i); + BlockState state = world.getBlockState(offset); + if (state.getBlock() instanceof PipeBlock) + return offset; + } + return null; + } + + public static boolean canPlace(World world, BlockPos pos, Direction direction) { + return getAttachedPipe(world, pos, direction) != null; } public int getAmount() { return this.dataManager.get(AMOUNT); } + @Override + public boolean attackEntityFrom(DamageSource source, float amount) { + if (this.isInvulnerableTo(source)) { + return false; + } else if (!source.isExplosion() && !this.getDisplayedItem().isEmpty()) { + if (!this.world.isRemote) { + this.dropItemOrSelf(source.getTrueSource(), false); + this.playSound(SoundEvents.ENTITY_ITEM_FRAME_REMOVE_ITEM, 1.0F, 1.0F); + } + + return true; + } else { + return super.attackEntityFrom(source, amount); + } + } + + @Override + public void onBroken(@Nullable Entity brokenEntity) { + this.playSound(SoundEvents.ENTITY_ITEM_FRAME_BREAK, 1.0F, 1.0F); + this.dropItemOrSelf(brokenEntity, true); + } + + private void dropItemOrSelf(@Nullable Entity entityIn, boolean b) { + if (!this.world.getGameRules().getBoolean(GameRules.DO_ENTITY_DROPS)) { + if (entityIn == null) + this.getDisplayedItem().setItemFrame(null); + } else { + ItemStack itemstack = this.getDisplayedItem(); + this.setDisplayedItem(ItemStack.EMPTY); + if (entityIn instanceof PlayerEntity) { + PlayerEntity playerentity = (PlayerEntity) entityIn; + if (playerentity.abilities.isCreativeMode) { + itemstack.setItemFrame(null); + return; + } + } + + if (b) + this.entityDropItem(Registry.pipeFrameItem); + + if (!itemstack.isEmpty()) { + itemstack = itemstack.copy(); + itemstack.setItemFrame(null); + this.entityDropItem(itemstack); + } + + } + } + + @Override + public boolean processInitialInteract(PlayerEntity player, Hand hand) { + if (this.getDisplayedItem().isEmpty()) + return super.processInitialInteract(player, hand); + return false; + } + @Override public IPacket createSpawnPacket() { return NetworkHooks.getEntitySpawningPacket(this); diff --git a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameRenderer.java b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameRenderer.java index 453608d..968b5dd 100644 --- a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameRenderer.java +++ b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameRenderer.java @@ -21,8 +21,6 @@ public class PipeFrameRenderer extends ItemFrameRenderer { @Override public void render(ItemFrameEntity entityIn, float entityYaw, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn) { super.render(entityIn, entityYaw, partialTicks, matrixStackIn, bufferIn, packedLightIn); - if (entityIn.getDisplayedItem().isEmpty()) - return; matrixStackIn.push(); Direction direction = entityIn.getHorizontalFacing(); Vec3d vec3d = this.getRenderOffset(entityIn, partialTicks); @@ -33,10 +31,10 @@ public class PipeFrameRenderer extends ItemFrameRenderer { FontRenderer font = this.getFontRendererFromRenderManager(); int amount = ((PipeFrameEntity) entityIn).getAmount(); - String ammountStrg = String.valueOf(amount); + String ammountStrg = amount < 0 ? "?" : String.valueOf(amount); float x = 0.5F - font.getStringWidth(ammountStrg) / 2F; Matrix4f matrix4f = matrixStackIn.getLast().getPositionMatrix(); - matrixStackIn.translate(0, -0.13F, 0); + matrixStackIn.translate(0, 0.285F, 0); matrixStackIn.scale(-0.02F, -0.02F, 0.02F); font.renderString(ammountStrg, x, 0, 0xFFFFFF, true, matrix4f, bufferIn, false, 0, packedLightIn); diff --git a/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java b/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java index 72e96e3..4ba09ee 100644 --- a/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java @@ -2,6 +2,7 @@ package de.ellpeck.prettypipes.items; import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.Registry; +import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.pipe.modules.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import net.minecraft.client.gui.screen.Screen; @@ -33,13 +34,7 @@ public abstract class ModuleItem extends Item implements IModule { @OnlyIn(Dist.CLIENT) public void addInformation(ItemStack stack, @Nullable World worldIn, List tooltip, ITooltipFlag flagIn) { super.addInformation(stack, worldIn, tooltip, flagIn); - if (Screen.hasShiftDown()) { - String[] content = I18n.format("info." + PrettyPipes.ID + "." + this.name).split("\n"); - for (String s : content) - tooltip.add(new StringTextComponent(s).setStyle(new Style().setColor(TextFormatting.GRAY))); - } else { - tooltip.add(new TranslationTextComponent("info." + PrettyPipes.ID + ".shift").setStyle(new Style().setColor(TextFormatting.DARK_GRAY))); - } + Utility.addTooltip(this.name, tooltip); } @Override diff --git a/src/main/java/de/ellpeck/prettypipes/items/PipeFrameItem.java b/src/main/java/de/ellpeck/prettypipes/items/PipeFrameItem.java index fa3a767..0548c5d 100644 --- a/src/main/java/de/ellpeck/prettypipes/items/PipeFrameItem.java +++ b/src/main/java/de/ellpeck/prettypipes/items/PipeFrameItem.java @@ -1,9 +1,11 @@ package de.ellpeck.prettypipes.items; import de.ellpeck.prettypipes.Registry; +import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.entities.PipeFrameEntity; import de.ellpeck.prettypipes.pipe.PipeBlock; import net.minecraft.block.BlockState; +import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.EntityType; import net.minecraft.entity.item.HangingEntity; import net.minecraft.entity.item.ItemFrameEntity; @@ -16,8 +18,12 @@ import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.ActionResultType; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; +import javax.annotation.Nullable; +import java.util.List; + public class PipeFrameItem extends Item { public PipeFrameItem() { super(new Properties().group(Registry.GROUP)); @@ -57,9 +63,12 @@ public class PipeFrameItem extends Item { } protected boolean canPlace(PlayerEntity playerIn, Direction directionIn, ItemStack itemStackIn, BlockPos posIn) { - BlockState state = playerIn.world.getBlockState(posIn.offset(directionIn.getOpposite())); - if (!(state.getBlock() instanceof PipeBlock)) - return false; - return !directionIn.getAxis().isVertical() && playerIn.canPlayerEdit(posIn, directionIn, itemStackIn); + return !directionIn.getAxis().isVertical() && playerIn.canPlayerEdit(posIn, directionIn, itemStackIn) && PipeFrameEntity.canPlace(playerIn.world, posIn, directionIn); + } + + @Override + public void addInformation(ItemStack stack, @Nullable World worldIn, List tooltip, ITooltipFlag flagIn) { + super.addInformation(stack, worldIn, tooltip, flagIn); + Utility.addTooltip(this.getRegistryName().getPath(), tooltip); } } diff --git a/src/main/resources/assets/prettypipes/lang/en_us.json b/src/main/resources/assets/prettypipes/lang/en_us.json index 30c7497..4c6945a 100644 --- a/src/main/resources/assets/prettypipes/lang/en_us.json +++ b/src/main/resources/assets/prettypipes/lang/en_us.json @@ -1,6 +1,8 @@ { "item.prettypipes.wrench": "Pipe Wrench", "item.prettypipes.blank_module": "Blank Module", + "item.prettypipes.pipe_frame": "Pipe Frame", + "info.prettypipes.pipe_frame": "Attach to a pipe or pipe-adjacent block\nShows amount of an item in the pipe's network", "item.prettypipes.low_extraction_module": "Low Extraction Module", "item.prettypipes.medium_extraction_module": "Medium Extraction Module", "item.prettypipes.high_extraction_module": "High Extraction Module", diff --git a/src/main/resources/assets/prettypipes/models/item/pipe_frame.json b/src/main/resources/assets/prettypipes/models/item/pipe_frame.json new file mode 100644 index 0000000..2924079 --- /dev/null +++ b/src/main/resources/assets/prettypipes/models/item/pipe_frame.json @@ -0,0 +1,6 @@ +{ + "parent": "item/generated", + "textures": { + "layer0": "prettypipes:item/pipe_frame" + } +} diff --git a/src/main/resources/assets/prettypipes/textures/item/pipe_frame.png b/src/main/resources/assets/prettypipes/textures/item/pipe_frame.png new file mode 100644 index 0000000000000000000000000000000000000000..e817f20cea2955d97f9c9a91d733efc9ad9e7850 GIT binary patch literal 365 zcmV-z0h0cSP)Px$CrLy>R5*>zk->(KzNF3yT&u;9QwtdZ(jRWE}D z0JnFKoKI$&QIV6&>)$&Y4tRZ!05AY~OO_wYCQC2ZB(K*1o}&{yy=?_lSUPl0PUYTK zxtC`t;`U7-Mn*0qnL6kNl0Q7n$>83t%_vLx0hoDoFGbZ`=(r(f5@DG