fix TemplateEntity's name, improve state container

This commit is contained in:
Meredith Espinosa 2019-06-19 17:51:11 -07:00
parent 8b8a86bed7
commit dd8c0a3d28
5 changed files with 23 additions and 16 deletions

View File

@ -1,7 +1,7 @@
package io.github.cottonmc.templates.block; package io.github.cottonmc.templates.block;
import io.github.cottonmc.templates.Templates; import io.github.cottonmc.templates.Templates;
import io.github.cottonmc.templates.block.entity.TemplateBlockEntity; import io.github.cottonmc.templates.block.entity.TemplateEntity;
import io.github.cottonmc.templates.util.StateContainer; import io.github.cottonmc.templates.util.StateContainer;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockEntityProvider;
@ -31,8 +31,8 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider
@Override @Override
public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (world.isClient || !(world.getBlockEntity(pos) instanceof TemplateBlockEntity)) return true; if (world.isClient || !(world.getBlockEntity(pos) instanceof TemplateEntity)) return true;
TemplateBlockEntity be = (TemplateBlockEntity) world.getBlockEntity(pos); TemplateEntity be = (TemplateEntity) world.getBlockEntity(pos);
ItemStack stack = player.getStackInHand(hand); ItemStack stack = player.getStackInHand(hand);
if (stack.getItem() instanceof BlockItem) { if (stack.getItem() instanceof BlockItem) {
Block block = ((BlockItem)stack.getItem()).getBlock(); Block block = ((BlockItem)stack.getItem()).getBlock();
@ -69,8 +69,8 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider
public void onBlockRemoved(BlockState state, World world, BlockPos pos, BlockState newState, boolean bool) { public void onBlockRemoved(BlockState state, World world, BlockPos pos, BlockState newState, boolean bool) {
if (newState.getBlock() == Templates.SLOPE) return; if (newState.getBlock() == Templates.SLOPE) return;
BlockEntity be = world.getBlockEntity(pos); BlockEntity be = world.getBlockEntity(pos);
if (be instanceof TemplateBlockEntity) { if (be instanceof TemplateEntity) {
TemplateBlockEntity template = (TemplateBlockEntity)be; TemplateEntity template = (TemplateEntity)be;
if (template.getRenderedState().getBlock() != Blocks.AIR) { if (template.getRenderedState().getBlock() != Blocks.AIR) {
ItemStack stack = new ItemStack(template.getRenderedState().getBlock()); ItemStack stack = new ItemStack(template.getRenderedState().getBlock());
ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack); ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack);
@ -93,8 +93,8 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider
@Override @Override
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos posFrom, boolean bool) { public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos posFrom, boolean bool) {
BlockEntity be = world.getBlockEntity(pos); BlockEntity be = world.getBlockEntity(pos);
if (be instanceof TemplateBlockEntity) { if (be instanceof TemplateEntity) {
TemplateBlockEntity template = (TemplateBlockEntity)be; TemplateEntity template = (TemplateEntity)be;
BlockState beState = template.getRenderedState(); BlockState beState = template.getRenderedState();
world.setBlockState(pos, state.with(LIGHT, template.hasGlowstone()? 15 : beState.getLuminance()).with(REDSTONE, template.hasRedstone() || beState.emitsRedstonePower())); world.setBlockState(pos, state.with(LIGHT, template.hasGlowstone()? 15 : beState.getLuminance()).with(REDSTONE, template.hasRedstone() || beState.emitsRedstonePower()));
} }
@ -113,8 +113,8 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider
@Override @Override
public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
BlockEntity be = view.getBlockEntity(pos); BlockEntity be = view.getBlockEntity(pos);
if (be instanceof TemplateBlockEntity) { if (be instanceof TemplateEntity) {
TemplateBlockEntity template = (TemplateBlockEntity)be; TemplateEntity template = (TemplateEntity)be;
if (template.hasRedstone()) return 15; if (template.hasRedstone()) return 15;
BlockState beState = template.getRenderedState(); BlockState beState = template.getRenderedState();
return beState.getWeakRedstonePower(view, pos, dir); return beState.getWeakRedstonePower(view, pos, dir);
@ -125,8 +125,8 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider
@Override @Override
public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
BlockEntity be = view.getBlockEntity(pos); BlockEntity be = view.getBlockEntity(pos);
if (be instanceof TemplateBlockEntity) { if (be instanceof TemplateEntity) {
TemplateBlockEntity template = (TemplateBlockEntity)be; TemplateEntity template = (TemplateEntity)be;
if (template.hasRedstone()) return 15; if (template.hasRedstone()) return 15;
BlockState beState = template.getRenderedState(); BlockState beState = template.getRenderedState();
return beState.getStrongRedstonePower(view, pos, dir); return beState.getStrongRedstonePower(view, pos, dir);
@ -137,7 +137,13 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider
@Override @Override
public BlockState getContainedState(World world, BlockPos pos) { public BlockState getContainedState(World world, BlockPos pos) {
BlockEntity be = world.getBlockEntity(pos); BlockEntity be = world.getBlockEntity(pos);
if (be instanceof TemplateBlockEntity) return ((TemplateBlockEntity)be).getRenderedState(); if (be instanceof TemplateEntity) return ((TemplateEntity)be).getRenderedState();
return Blocks.AIR.getDefaultState(); return Blocks.AIR.getDefaultState();
} }
@Override
public void setContainedState(World world, BlockPos pos, BlockState state) {
BlockEntity be = world.getBlockEntity(pos);
if (be instanceof TemplateEntity) ((TemplateEntity)be).setRenderedState(state);
}
} }

View File

@ -2,7 +2,7 @@ package io.github.cottonmc.templates.block.entity;
import io.github.cottonmc.templates.Templates; import io.github.cottonmc.templates.Templates;
public class SlopeEntity extends TemplateBlockEntity { public class SlopeEntity extends TemplateEntity {
public SlopeEntity() { public SlopeEntity() {
super(Templates.SLOPE_ENTITY, Templates.SLOPE); super(Templates.SLOPE_ENTITY, Templates.SLOPE);
} }

View File

@ -13,13 +13,13 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
public abstract class TemplateBlockEntity extends BlockEntity implements BlockEntityClientSerializable, RenderAttachmentBlockEntity { public abstract class TemplateEntity extends BlockEntity implements BlockEntityClientSerializable, RenderAttachmentBlockEntity {
protected BlockState renderedState = Blocks.AIR.getDefaultState(); protected BlockState renderedState = Blocks.AIR.getDefaultState();
protected boolean glowstone = false; protected boolean glowstone = false;
protected boolean redstone = false; protected boolean redstone = false;
private Block baseBlock; private Block baseBlock;
public TemplateBlockEntity(BlockEntityType<?> type, Block baseBlock) { public TemplateEntity(BlockEntityType<?> type, Block baseBlock) {
super(type); super(type);
this.baseBlock = baseBlock; this.baseBlock = baseBlock;
} }

View File

@ -23,7 +23,7 @@ public class TemplateModelVariantProvider implements ModelVariantProvider {
return variants.get(modelId); return variants.get(modelId);
} }
public void registerTemplateModels(Block block, BlockState itemState, Function<BlockState, SimpleModel> model) { public void registerTemplateModels(Block block, BlockState itemState, Function<BlockState, AbstractModel> model) {
for (BlockState state : block.getStateFactory().getStates()) { for (BlockState state : block.getStateFactory().getStates()) {
variants.put(BlockModels.getModelId(state), (SimpleUnbakedModel)() -> model.apply(state)); variants.put(BlockModels.getModelId(state), (SimpleUnbakedModel)() -> model.apply(state));
} }

View File

@ -6,4 +6,5 @@ import net.minecraft.world.World;
public interface StateContainer { public interface StateContainer {
BlockState getContainedState(World world, BlockPos pos); BlockState getContainedState(World world, BlockPos pos);
void setContainedState(World world, BlockPos pos, BlockState state);
} }