From 46ef0ef0a98ab3e6088af270184800868e48c126 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 13 Apr 2020 22:54:18 +0200 Subject: [PATCH] added pipe wrench --- build.gradle | 8 -- .../java/de/ellpeck/prettypipes/Registry.java | 16 +++- .../ellpeck/prettypipes/blocks/PipeBlock.java | 50 +++++++------ .../ellpeck/prettypipes/items/WrenchItem.java | 69 ++++++++++++++++++ .../assets/prettypipes/lang/en_us.json | 5 ++ .../prettypipes/models/item/wrench.json | 6 ++ .../prettypipes/textures/item/wrench.png | Bin 0 -> 284 bytes .../prettypipes/loot_tables/blocks/pipe.json | 19 +++++ 8 files changed, 143 insertions(+), 30 deletions(-) create mode 100644 src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java create mode 100644 src/main/resources/assets/prettypipes/lang/en_us.json create mode 100644 src/main/resources/assets/prettypipes/models/item/wrench.json create mode 100644 src/main/resources/assets/prettypipes/textures/item/wrench.png create mode 100644 src/main/resources/data/prettypipes/loot_tables/blocks/pipe.json diff --git a/build.gradle b/build.gradle index 50e474b..356a872 100644 --- a/build.gradle +++ b/build.gradle @@ -83,20 +83,13 @@ sourceSets.main.resources { } repositories { - mavenCentral() maven { url = "https://dvs1.progwml6.com/files/maven" } } -configurations { - embed - compile.extendsFrom(embed) -} - dependencies { minecraft 'net.minecraftforge:forge:1.15.2-31.1.19' - embed "org.jgrapht:jgrapht-core:1.4.0" compileOnly fg.deobf("mezz.jei:jei-1.15.2:6.0.0.2:api") runtimeOnly fg.deobf("mezz.jei:jei-1.15.2:6.0.0.2") @@ -115,7 +108,6 @@ jar { "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") ]) } - from configurations.embed.collect { it.isDirectory() ? it : zipTree(it) } } task deobfJar(type: Jar) { diff --git a/src/main/java/de/ellpeck/prettypipes/Registry.java b/src/main/java/de/ellpeck/prettypipes/Registry.java index b2a127f..0189bf2 100644 --- a/src/main/java/de/ellpeck/prettypipes/Registry.java +++ b/src/main/java/de/ellpeck/prettypipes/Registry.java @@ -1,12 +1,14 @@ package de.ellpeck.prettypipes; import de.ellpeck.prettypipes.blocks.PipeBlock; +import de.ellpeck.prettypipes.items.WrenchItem; import net.minecraft.block.Block; import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderTypeLookup; import net.minecraft.item.BlockItem; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; +import net.minecraft.item.ItemStack; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @@ -18,6 +20,14 @@ import net.minecraftforge.registries.ForgeRegistries; @Mod.EventBusSubscriber(bus = Bus.MOD) public final class Registry { + public static final ItemGroup GROUP = new ItemGroup(PrettyPipes.ID) { + @Override + public ItemStack createIcon() { + return new ItemStack(wrench); + } + }; + + public static Item wrench; public static Block pipe; @SubscribeEvent @@ -29,9 +39,13 @@ public final class Registry { @SubscribeEvent public static void registerItems(RegistryEvent.Register event) { + event.getRegistry().registerAll( + wrench = new WrenchItem().setRegistryName("wrench") + ); + ForgeRegistries.BLOCKS.getValues().stream() .filter(b -> b.getRegistryName().getNamespace().equals(PrettyPipes.ID)) - .forEach(b -> event.getRegistry().register(new BlockItem(b, new Item.Properties().group(ItemGroup.MISC)).setRegistryName(b.getRegistryName()))); + .forEach(b -> event.getRegistry().register(new BlockItem(b, new Item.Properties().group(GROUP)).setRegistryName(b.getRegistryName()))); } public static void setup(FMLCommonSetupEvent event) { diff --git a/src/main/java/de/ellpeck/prettypipes/blocks/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/blocks/PipeBlock.java index 24ae8f4..0486d44 100644 --- a/src/main/java/de/ellpeck/prettypipes/blocks/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/blocks/PipeBlock.java @@ -12,6 +12,7 @@ import net.minecraft.state.EnumProperty; import net.minecraft.state.StateContainer; import net.minecraft.util.Direction; import net.minecraft.util.IStringSerializable; +import net.minecraft.util.datafix.fixes.BlockEntityKeepPacked; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.VoxelShape; @@ -29,7 +30,7 @@ public class PipeBlock extends Block { public static final Map> DIRECTIONS = new HashMap<>(); private static final Map SHAPE_CACHE = new HashMap<>(); private static final VoxelShape CENTER_SHAPE = makeCuboidShape(5, 5, 5, 11, 11, 11); - private static final Map DIR_SHAPES = ImmutableMap.builder() + public static final Map DIR_SHAPES = ImmutableMap.builder() .put(Direction.UP, makeCuboidShape(5, 10, 5, 11, 16, 11)) .put(Direction.DOWN, makeCuboidShape(5, 0, 5, 11, 6, 11)) .put(Direction.NORTH, makeCuboidShape(5, 5, 0, 11, 11, 6)) @@ -59,7 +60,7 @@ public class PipeBlock extends Block { @Override public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) { - BlockState newState = this.createState(worldIn, pos); + BlockState newState = this.createState(worldIn, pos, state); if (newState != state) worldIn.setBlockState(pos, newState); } @@ -67,24 +68,7 @@ public class PipeBlock extends Block { @Nullable @Override public BlockState getStateForPlacement(BlockItemUseContext context) { - return this.createState(context.getWorld(), context.getPos()); - } - - private BlockState createState(World world, BlockPos pos) { - BlockState state = this.getDefaultState(); - for (Map.Entry> entry : DIRECTIONS.entrySet()) { - BlockPos neighborPos = pos.offset(entry.getKey()); - boolean canConnect = this.canConnect(world, neighborPos); - state = state.with(entry.getValue(), canConnect ? ConnectionType.CONNECTED : ConnectionType.DISCONNECTED); - } - return state; - } - - private boolean canConnect(World world, BlockPos offset) { - if (!world.isBlockLoaded(offset)) - return false; - BlockState state = world.getBlockState(offset); - return state.getBlock() == this; + return this.createState(context.getWorld(), context.getPos(), this.getDefaultState()); } @Override @@ -107,9 +91,33 @@ public class PipeBlock extends Block { return shape; } + private BlockState createState(World world, BlockPos pos, BlockState current) { + BlockState state = this.getDefaultState(); + for (Map.Entry> entry : DIRECTIONS.entrySet()) { + ConnectionType type = getConnectionType(world, pos, entry.getKey()); + if (type == ConnectionType.CONNECTED && current.get(entry.getValue()) == ConnectionType.BLOCKED) + type = ConnectionType.BLOCKED; + state = state.with(entry.getValue(), type); + } + return state; + } + + public static ConnectionType getConnectionType(World world, BlockPos pos, Direction direction) { + BlockPos offset = pos.offset(direction); + if (!world.isBlockLoaded(offset)) + return ConnectionType.DISCONNECTED; + BlockState state = world.getBlockState(offset); + if (!(state.getBlock() instanceof PipeBlock)) + return ConnectionType.DISCONNECTED; + if (state.get(DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED) + return ConnectionType.BLOCKED; + return ConnectionType.CONNECTED; + } + public enum ConnectionType implements IStringSerializable { CONNECTED, - DISCONNECTED; + DISCONNECTED, + BLOCKED; private final String name; diff --git a/src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java b/src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java new file mode 100644 index 0000000..e939dcd --- /dev/null +++ b/src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java @@ -0,0 +1,69 @@ +package de.ellpeck.prettypipes.items; + +import de.ellpeck.prettypipes.Registry; +import de.ellpeck.prettypipes.blocks.PipeBlock; +import de.ellpeck.prettypipes.blocks.PipeBlock.ConnectionType; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUseContext; +import net.minecraft.state.EnumProperty; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ActionResultType; +import net.minecraft.util.Direction; +import net.minecraft.util.SoundCategory; +import net.minecraft.util.SoundEvents; +import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.util.math.shapes.VoxelShapes; +import net.minecraft.world.World; + +import java.util.Map; + +public class WrenchItem extends Item { + public WrenchItem() { + super(new Item.Properties().maxStackSize(1).group(Registry.GROUP)); + } + + @Override + public ActionResultType onItemUse(ItemUseContext context) { + World world = context.getWorld(); + BlockPos pos = context.getPos(); + BlockState state = world.getBlockState(pos); + if (!(state.getBlock() instanceof PipeBlock)) + return ActionResultType.PASS; + + if (context.getPlayer().isShiftKeyDown()) { + if (!world.isRemote) { + Block.spawnDrops(state, world, pos, world.getTileEntity(pos), null, ItemStack.EMPTY); + world.playSound(null, pos, SoundEvents.ENTITY_ITEM_FRAME_REMOVE_ITEM, SoundCategory.PLAYERS, 1, 1); + world.removeBlock(pos, false); + } + return ActionResultType.SUCCESS; + } + + // Blocking + for (Map.Entry entry : PipeBlock.DIR_SHAPES.entrySet()) { + AxisAlignedBB box = entry.getValue().getBoundingBox().offset(pos).grow(0.001F); + if (!box.contains(context.getHitVec())) + continue; + EnumProperty prop = PipeBlock.DIRECTIONS.get(entry.getKey()); + ConnectionType curr = state.get(prop); + if (curr == ConnectionType.DISCONNECTED) + continue; + + if (!world.isRemote) { + ConnectionType newType = curr == ConnectionType.BLOCKED ? ConnectionType.CONNECTED : ConnectionType.BLOCKED; + BlockState newState = state.with(prop, newType); + world.setBlockState(pos, newState); + + BlockPos otherPos = pos.offset(entry.getKey()); + world.setBlockState(otherPos, world.getBlockState(otherPos).with(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType)); + } + return ActionResultType.SUCCESS; + } + return ActionResultType.PASS; + } +} diff --git a/src/main/resources/assets/prettypipes/lang/en_us.json b/src/main/resources/assets/prettypipes/lang/en_us.json new file mode 100644 index 0000000..76ea429 --- /dev/null +++ b/src/main/resources/assets/prettypipes/lang/en_us.json @@ -0,0 +1,5 @@ +{ + "item.prettypipes.wrench": "Pipe Wrench", + "block.prettypipes.pipe": "Pipe", + "itemGroup.prettypipes": "Pretty Pipes" +} \ No newline at end of file diff --git a/src/main/resources/assets/prettypipes/models/item/wrench.json b/src/main/resources/assets/prettypipes/models/item/wrench.json new file mode 100644 index 0000000..d291a12 --- /dev/null +++ b/src/main/resources/assets/prettypipes/models/item/wrench.json @@ -0,0 +1,6 @@ +{ + "parent": "item/handheld", + "textures": { + "layer0": "prettypipes:item/wrench" + } +} diff --git a/src/main/resources/assets/prettypipes/textures/item/wrench.png b/src/main/resources/assets/prettypipes/textures/item/wrench.png new file mode 100644 index 0000000000000000000000000000000000000000..a1355e9983796c6983ff2680a7e799e5cdb32e39 GIT binary patch literal 284 zcmV+%0ptFOP)Px#)=5M`R5*>jkIf1JQ5c4w;hfDVEM0-JJeQ)B-AMN0f~1s%Qr0fPCAb5N5j7fS zsAHisnLj^MysOjsdfp%Sr^ch99`qeu2+T7n6)}01^{K(2@91i+&fayAQZSj$tdAR* z%6r!Z;N*GH?X5w#)7Ajj>kWY5a%QnyMa|2v;I&d(9gb`&HbTf03?Q-~^A5t0!|jnQ zr69PZLt<`grSvld(6DWSFl63prqW{uCVA)k^tM|o?=Pun!p`>rVz6nG2d5@vO?d@p i8hiuCCKx5B{9JcID}Cw={*+Mw0000