You can now place feathers (and some other choice items) into flower pots

This commit is contained in:
Sollace 2019-02-24 01:04:18 +02:00
parent fd5004b1a2
commit 86c336dcee
8 changed files with 228 additions and 22 deletions

View file

@ -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> 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;
}
}
}

View file

@ -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<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
return new StateImplementation(block, properties) {
@Override
public <T extends Comparable<T>, V extends T> IBlockState withProperty(IProperty<T> property, V value) {
if (property == TYPE && !getProperties().containsKey(property)) {
return this;
}
return super.withProperty(property, value);
}
};
}
};
return new LenientState(this, STAGE);
}
}

View file

@ -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,

View file

@ -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;
}

View file

@ -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<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
return new Impl(block, properties);
}
class Impl extends StateImplementation {
protected Impl(Block blockIn, ImmutableMap<IProperty<?>, Comparable<?>> propertiesIn) {
super(blockIn, propertiesIn);
}
@Override
public <T extends Comparable<T>, V extends T> IBlockState withProperty(IProperty<T> property, V value) {
if (!getProperties().containsKey(property)) {
return this;
}
return super.withProperty(property, value);
}
}
}

View file

@ -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" }
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "minecraft:block/flower_pot_cross",
"textures": {
"plant": "minecraft:items/feather"
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "minecraft:block/flower_pot_cross",
"textures": {
"plant": "unicopia:items/staff_meadow_brook"
}
}