From 86c336dceee34cb0a8965f828c2826644e2a7fd5 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 24 Feb 2019 01:04:18 +0200 Subject: [PATCH] You can now place feathers (and some other choice items) into flower pots --- .../minelittlepony/unicopia/block/UPot.java | 157 ++++++++++++++++++ .../unicopia/block/USapling.java | 23 +-- .../minelittlepony/unicopia/init/UBlocks.java | 4 +- .../unicopia/world/BlockInteractions.java | 7 + .../com/minelittlepony/util/LenientState.java | 40 +++++ .../unicopia/blockstates/flower_pot.json | 7 + .../models/block/flower_pot_feather.json | 6 + .../models/block/flower_pot_meadowbrook.json | 6 + 8 files changed, 228 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/block/UPot.java create mode 100644 src/main/java/com/minelittlepony/util/LenientState.java create mode 100644 src/main/resources/assets/unicopia/blockstates/flower_pot.json create mode 100644 src/main/resources/assets/unicopia/models/block/flower_pot_feather.json create mode 100644 src/main/resources/assets/unicopia/models/block/flower_pot_meadowbrook.json diff --git a/src/main/java/com/minelittlepony/unicopia/block/UPot.java b/src/main/java/com/minelittlepony/unicopia/block/UPot.java new file mode 100644 index 00000000..2f232169 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/block/UPot.java @@ -0,0 +1,157 @@ +package com.minelittlepony.unicopia.block; + +import javax.annotation.Nullable; + +import com.minelittlepony.util.LenientState; + +import net.minecraft.block.BlockFlowerPot; +import net.minecraft.block.SoundType; +import net.minecraft.block.properties.PropertyEnum; +import net.minecraft.block.state.BlockStateContainer; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; +import net.minecraft.stats.StatList; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.tileentity.TileEntityFlowerPot; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.IStringSerializable; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; + +public class UPot extends BlockFlowerPot { + + public static final PropertyEnum PLANT = PropertyEnum.create("plant", Plant.class); + + public UPot(String domain, String name) { + setTranslationKey(name); + setRegistryName(domain, name); + setHardness(0); + setSoundType(SoundType.STONE); + } + + public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { + ItemStack itemstack = player.getHeldItem(hand); + TileEntityFlowerPot tile = getTileEntity(world, pos); + + if (tile == null) { + return false; + } + + ItemStack current = tile.getFlowerItemStack(); + IBlockState newState = state; + + if (current.isEmpty()) { + Plant contents = getContentType(itemstack); + + if (contents.isEmpty()) { + return false; + } + + tile.setItemStack(itemstack); + + if (world.getBlockState(pos).getBlock() != this) { + newState = getDefaultState().withProperty(PLANT, contents); + + world.setBlockState(pos, newState, 0); + tile = getTileEntity(world, pos); + tile.setItemStack(itemstack); + } + + player.addStat(StatList.FLOWER_POTTED); + + if (!player.capabilities.isCreativeMode) { + itemstack.shrink(1); + } + } else { + if (itemstack.isEmpty()) { + player.setHeldItem(hand, current); + } else if (!player.addItemStackToInventory(current)) { + player.dropItem(current, false); + } + + tile.setItemStack(ItemStack.EMPTY); + world.setBlockState(pos, Blocks.FLOWER_POT.getDefaultState(), 0); + } + + tile.markDirty(); + world.notifyBlockUpdate(pos, state, newState, 3); + + return true; + } + + @Nullable + protected TileEntityFlowerPot getTileEntity(IBlockAccess world, BlockPos pos) { + TileEntity tileentity = world.getTileEntity(pos); + + return tileentity instanceof TileEntityFlowerPot ? (TileEntityFlowerPot)tileentity : null; + } + + protected Plant getContentType(ItemStack stack) { + for (Plant i : Plant.values()) { + if (i.matches(stack)) { + return i; + } + } + + return Plant.EMPTY; + } + + public TileEntity createNewTileEntity(World worldIn, int meta) { + return new TileEntityFlowerPot(Items.AIR, 0); + } + + @Override + protected BlockStateContainer createBlockState() { + return new LenientState(this, PLANT); + } + + @Override + public int getMetaFromState(IBlockState state) { + return 0; + } + + @Override + public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) { + TileEntityFlowerPot tile = getTileEntity(world, pos); + + return state.withProperty(PLANT, tile == null ? Plant.EMPTY : getContentType(tile.getFlowerItemStack())); + } + + enum Plant implements IStringSerializable { + EMPTY("minecraft:air", -1), + QUILL("minecraft:feather", -1), + MEADOWBROOK("unicopia:staff_meadow_brook", -1); + + private final int damage; + private final ResourceLocation item; + + Plant(String item, int damage) { + this.damage = damage; + this.item = new ResourceLocation(item); + } + + @Override + public String getName() { + return name().toLowerCase(); + } + + public boolean matches(ItemStack stack) { + if (damage >= 0 && stack.getItemDamage() != damage) { + return false; + } + + return !stack.isEmpty() && stack.getItem().getRegistryName().equals(item); + } + + public boolean isEmpty() { + return this == EMPTY; + } + + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/block/USapling.java b/src/main/java/com/minelittlepony/unicopia/block/USapling.java index 77e8f1eb..9ff54455 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/USapling.java +++ b/src/main/java/com/minelittlepony/unicopia/block/USapling.java @@ -1,17 +1,14 @@ package com.minelittlepony.unicopia.block; -import java.util.Optional; import java.util.Random; import javax.annotation.Nullable; -import com.google.common.collect.ImmutableMap; +import com.minelittlepony.util.LenientState; -import net.minecraft.block.Block; import net.minecraft.block.BlockPlanks; import net.minecraft.block.BlockSapling; import net.minecraft.block.SoundType; -import net.minecraft.block.properties.IProperty; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; @@ -21,7 +18,6 @@ import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.gen.feature.WorldGenAbstractTree; -import net.minecraftforge.common.property.IUnlistedProperty; public class USapling extends BlockSapling implements ITreeGen { @@ -158,21 +154,6 @@ public class USapling extends BlockSapling implements ITreeGen { @Override protected BlockStateContainer createBlockState() { - return new BlockStateContainer(this, new IProperty[] {STAGE}) { - @Override - protected StateImplementation createState(Block block, ImmutableMap, Comparable> properties, @Nullable ImmutableMap, Optional> unlistedProperties) { - return new StateImplementation(block, properties) { - @Override - public , V extends T> IBlockState withProperty(IProperty property, V value) { - if (property == TYPE && !getProperties().containsKey(property)) { - return this; - } - - return super.withProperty(property, value); - } - }; - } - - }; + return new LenientState(this, STAGE); } } diff --git a/src/main/java/com/minelittlepony/unicopia/init/UBlocks.java b/src/main/java/com/minelittlepony/unicopia/init/UBlocks.java index 0d4eb2d5..b6251905 100644 --- a/src/main/java/com/minelittlepony/unicopia/init/UBlocks.java +++ b/src/main/java/com/minelittlepony/unicopia/init/UBlocks.java @@ -12,6 +12,7 @@ import com.minelittlepony.unicopia.block.BlockCloudStairs; import com.minelittlepony.unicopia.block.BlockSugar; import com.minelittlepony.unicopia.block.BlockTomatoPlant; import com.minelittlepony.unicopia.block.IColourful; +import com.minelittlepony.unicopia.block.UPot; import com.minelittlepony.unicopia.block.USapling; import com.minelittlepony.unicopia.item.ItemApple; import com.minelittlepony.unicopia.block.BlockCloudDoor; @@ -60,6 +61,7 @@ public class UBlocks { public static final BlockCloudFarm cloud_farmland = new BlockCloudFarm(Unicopia.MODID, "cloud_farmland"); public static final Block sugar_block = new BlockSugar(Unicopia.MODID, "sugar_block"); + public static final UPot flower_pot = new UPot(Unicopia.MODID, "flower_pot"); public static final USapling apple_tree = new USapling(Unicopia.MODID, "apple_sapling") .setTreeGen((w, s, m) -> new WorldGenTrees(true, 5, Blocks.LOG.getDefaultState(), UBlocks.apple_leaves.getDefaultState(), false)); @@ -77,7 +79,7 @@ public class UBlocks { packed_cloud_slab, packed_cloud_slab.doubleSlab, cloud_fence, cloud_banister, mist_door, anvil, cloud_farmland, - sugar_block, + sugar_block, flower_pot, alfalfa, tomato_plant, enchanted_torch, diff --git a/src/main/java/com/minelittlepony/unicopia/world/BlockInteractions.java b/src/main/java/com/minelittlepony/unicopia/world/BlockInteractions.java index 3fbc421f..180020b9 100644 --- a/src/main/java/com/minelittlepony/unicopia/world/BlockInteractions.java +++ b/src/main/java/com/minelittlepony/unicopia/world/BlockInteractions.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.world; import java.util.List; import com.minelittlepony.unicopia.block.ITillable; +import com.minelittlepony.unicopia.init.UBlocks; import com.minelittlepony.unicopia.init.UItems; import com.minelittlepony.unicopia.spell.SpellRegistry; @@ -50,6 +51,12 @@ public class BlockInteractions { } } + if (state.getBlock() == Blocks.FLOWER_POT) { + if (UBlocks.flower_pot.onBlockActivated(world, pos, state, player, hand, player.getHorizontalFacing(), 0.5F, 0.5F, 0.5F)) { + return EnumActionResult.SUCCESS; + } + } + return EnumActionResult.PASS; } diff --git a/src/main/java/com/minelittlepony/util/LenientState.java b/src/main/java/com/minelittlepony/util/LenientState.java new file mode 100644 index 00000000..7f3015b0 --- /dev/null +++ b/src/main/java/com/minelittlepony/util/LenientState.java @@ -0,0 +1,40 @@ +package com.minelittlepony.util; + +import java.util.Optional; + +import javax.annotation.Nullable; + +import com.google.common.collect.ImmutableMap; + +import net.minecraft.block.Block; +import net.minecraft.block.properties.IProperty; +import net.minecraft.block.state.BlockStateContainer; +import net.minecraft.block.state.IBlockState; +import net.minecraftforge.common.property.IUnlistedProperty; + +public class LenientState extends BlockStateContainer { + + public LenientState(Block blockIn, IProperty... properties) { + super(blockIn, properties); + } + + @Override + protected StateImplementation createState(Block block, ImmutableMap, Comparable> properties, @Nullable ImmutableMap, Optional> unlistedProperties) { + return new Impl(block, properties); + } + + class Impl extends StateImplementation { + protected Impl(Block blockIn, ImmutableMap, Comparable> propertiesIn) { + super(blockIn, propertiesIn); + } + + @Override + public , V extends T> IBlockState withProperty(IProperty property, V value) { + if (!getProperties().containsKey(property)) { + return this; + } + + return super.withProperty(property, value); + } + } +} diff --git a/src/main/resources/assets/unicopia/blockstates/flower_pot.json b/src/main/resources/assets/unicopia/blockstates/flower_pot.json new file mode 100644 index 00000000..5836f110 --- /dev/null +++ b/src/main/resources/assets/unicopia/blockstates/flower_pot.json @@ -0,0 +1,7 @@ +{ + "variants": { + "plant=none": { "model": "minecraft:flower_pot" }, + "plant=quill": { "model": "unicopia:flower_pot_feather" }, + "plant=meadowbrook": { "model": "unicopia:flower_pot_meadowbrook" } + } +} diff --git a/src/main/resources/assets/unicopia/models/block/flower_pot_feather.json b/src/main/resources/assets/unicopia/models/block/flower_pot_feather.json new file mode 100644 index 00000000..91196422 --- /dev/null +++ b/src/main/resources/assets/unicopia/models/block/flower_pot_feather.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/flower_pot_cross", + "textures": { + "plant": "minecraft:items/feather" + } +} diff --git a/src/main/resources/assets/unicopia/models/block/flower_pot_meadowbrook.json b/src/main/resources/assets/unicopia/models/block/flower_pot_meadowbrook.json new file mode 100644 index 00000000..7e90a89a --- /dev/null +++ b/src/main/resources/assets/unicopia/models/block/flower_pot_meadowbrook.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/flower_pot_cross", + "textures": { + "plant": "unicopia:items/staff_meadow_brook" + } +}