This commit is contained in:
Aleksey 2021-02-27 00:39:17 +03:00
parent db2a555ae9
commit 616c824257
6 changed files with 123 additions and 125 deletions

View file

@ -104,7 +104,7 @@ public class PedestalBlock extends BlockBaseNotFull implements BlockEntityProvid
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (world.isClient || !state.isOf(this)) return ActionResult.CONSUME;
if (!this.isPlaceable(state)) {
if (!isPlaceable(state)) {
return ActionResult.PASS;
}
BlockEntity blockEntity = world.getBlockEntity(pos);
@ -114,12 +114,12 @@ public class PedestalBlock extends BlockBaseNotFull implements BlockEntityProvid
ItemStack itemStack = player.getStackInHand(hand);
if (itemStack.isEmpty()) return ActionResult.CONSUME;
pedestal.setStack(0, itemStack.split(1));
this.checkRitual(world, pos);
checkRitual(world, pos);
return ActionResult.SUCCESS;
} else {
ItemStack itemStack = pedestal.getStack(0);
if (player.giveItemStack(itemStack)) {
pedestal.removeStack(world, state);
pedestal.removeStack(0);
return ActionResult.SUCCESS;
}
return ActionResult.FAIL;
@ -151,25 +151,25 @@ public class PedestalBlock extends BlockBaseNotFull implements BlockEntityProvid
boolean hasPedestalOver = upState.getBlock() instanceof PedestalBlock;
boolean hasPedestalUnder = downState.getBlock() instanceof PedestalBlock;
if (!hasPedestalOver && hasPedestalUnder && upSideSolid) {
return this.getDefaultState().with(STATE, PedestalState.COLUMN_TOP);
return getDefaultState().with(STATE, PedestalState.COLUMN_TOP);
} else if (!hasPedestalOver && !hasPedestalUnder && upSideSolid) {
return this.getDefaultState().with(STATE, PedestalState.COLUMN);
return getDefaultState().with(STATE, PedestalState.COLUMN);
} else if (hasPedestalUnder && hasPedestalOver) {
return this.getDefaultState().with(STATE, PedestalState.PILLAR);
return getDefaultState().with(STATE, PedestalState.PILLAR);
} else if (hasPedestalUnder) {
return this.getDefaultState().with(STATE, PedestalState.PEDESTAL_TOP);
return getDefaultState().with(STATE, PedestalState.PEDESTAL_TOP);
} else if (hasPedestalOver) {
return this.getDefaultState().with(STATE, PedestalState.BOTTOM);
return getDefaultState().with(STATE, PedestalState.BOTTOM);
}
return this.getDefaultState();
return getDefaultState();
}
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
BlockState updated = this.getUpdatedState(state, direction, newState, world, pos, posFrom);
BlockState updated = getUpdatedState(state, direction, newState, world, pos, posFrom);
if (!updated.isOf(this)) return updated;
if (!this.isPlaceable(updated)) {
this.moveStoredStack(world, updated, pos);
if (!isPlaceable(updated)) {
moveStoredStack(world, updated, pos);
}
return updated;
}
@ -185,21 +185,27 @@ public class PedestalBlock extends BlockBaseNotFull implements BlockEntityProvid
if (direction == Direction.UP) {
upSideSolid = newState.isSideSolidFullSquare(world, posFrom, Direction.DOWN) || newState.isIn(BlockTags.WALLS);
hasPedestalOver = newState.getBlock() instanceof PedestalBlock;
} else if (direction == Direction.DOWN) {
} else {
hasPedestalUnder = newState.getBlock() instanceof PedestalBlock;
}
BlockState updatedState;
if (!hasPedestalOver && hasPedestalUnder && upSideSolid) {
return state.with(STATE, PedestalState.COLUMN_TOP);
updatedState = state.with(STATE, PedestalState.COLUMN_TOP);
} else if (!hasPedestalOver && !hasPedestalUnder && upSideSolid) {
return state.with(STATE, PedestalState.COLUMN);
updatedState = state.with(STATE, PedestalState.COLUMN);
} else if (hasPedestalUnder && hasPedestalOver) {
return state.with(STATE, PedestalState.PILLAR);
updatedState = state.with(STATE, PedestalState.PILLAR);
} else if (hasPedestalUnder) {
return state.with(STATE, PedestalState.PEDESTAL_TOP);
updatedState = state.with(STATE, PedestalState.PEDESTAL_TOP);
} else if (hasPedestalOver) {
return state.with(STATE, PedestalState.BOTTOM);
updatedState = state.with(STATE, PedestalState.BOTTOM);
} else {
updatedState = state.with(STATE, PedestalState.DEFAULT);
}
return state.with(STATE, PedestalState.DEFAULT);
if (!isPlaceable(updatedState)) {
updatedState = updatedState.with(HAS_ITEM, false).with(HAS_LIGHT, false);
}
return updatedState;
}
@Override
@ -208,7 +214,7 @@ public class PedestalBlock extends BlockBaseNotFull implements BlockEntityProvid
if (state.isOf(this)) {
if (isPlaceable(state)) {
BlockEntity blockEntity = builder.getNullable(LootContextParameters.BLOCK_ENTITY);
if (blockEntity != null && blockEntity instanceof PedestalBlockEntity) {
if (blockEntity instanceof PedestalBlockEntity) {
PedestalBlockEntity pedestal = (PedestalBlockEntity) blockEntity;
if (!pedestal.isEmpty()) {
drop.add(pedestal.getStack(0));
@ -222,63 +228,61 @@ public class PedestalBlock extends BlockBaseNotFull implements BlockEntityProvid
}
private void moveStoredStack(WorldAccess world, BlockState state, BlockPos pos) {
ItemStack stack = ItemStack.EMPTY;
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof PedestalBlockEntity && state.isOf(this)) {
PedestalBlockEntity pedestal = (PedestalBlockEntity) blockEntity;
stack = pedestal.getStack(0);
pedestal.clear();
BlocksHelper.setWithoutUpdate(world, pos, state.with(HAS_ITEM, false));
}
if (!stack.isEmpty()) {
BlockPos upPos = pos.up();
this.moveStoredStack(world, stack, world.getBlockState(upPos), upPos);
ItemStack stack = pedestal.removeStack(0);
if (!stack.isEmpty()) {
moveStoredStack(blockEntity, world, stack, pos.up());
}
}
}
private void moveStoredStack(WorldAccess world, ItemStack stack, BlockState state, BlockPos pos) {
BlockEntity blockEntity = world.getBlockEntity(pos);
private void moveStoredStack(BlockEntity blockEntity, WorldAccess world, ItemStack stack, BlockPos pos) {
BlockState state = world.getBlockState(pos);
if (!state.isOf(this)) {
this.dropStoredStack(blockEntity, stack, pos);
dropStoredStack(blockEntity, stack, pos);
} else if (state.get(STATE).equals(PedestalState.PILLAR)) {
BlockPos upPos = pos.up();
this.moveStoredStack(world, stack, world.getBlockState(upPos), upPos);
} else if (!this.isPlaceable(state)) {
this.dropStoredStack(blockEntity, stack, pos);
moveStoredStack(blockEntity, world, stack, pos.up());
} else if (!isPlaceable(state)) {
dropStoredStack(blockEntity, stack, pos);
} else if (blockEntity instanceof PedestalBlockEntity) {
PedestalBlockEntity pedestal = (PedestalBlockEntity) blockEntity;
if (pedestal.isEmpty()) {
pedestal.setStack(0, stack);
} else {
this.dropStoredStack(blockEntity, stack, pos);
dropStoredStack(blockEntity, stack, pos);
}
} else {
this.dropStoredStack(blockEntity, stack, pos);
dropStoredStack(blockEntity, stack, pos);
}
}
private void dropStoredStack(BlockEntity blockEntity, ItemStack stack, BlockPos pos) {
if (blockEntity != null && blockEntity.getWorld() != null) {
World world = blockEntity.getWorld();
Block.dropStack(world, this.getDropPos(world, pos), stack);
Block.dropStack(world, getDropPos(world, pos), stack);
}
}
private BlockPos getDropPos(WorldAccess world, BlockPos pos) {
BlockPos dropPos;
if (world.getBlockState(pos).isAir()) {
return pos;
}
if (world.getBlockState(pos.up()).isAir()) {
return pos.up();
}
for(int i = 2; i < Direction.values().length; i++) {
dropPos = pos.offset(Direction.byId(i));
if (world.getBlockState(dropPos).isAir()) {
return dropPos.toImmutable();
}
}
if (world.getBlockState(pos.up()).isAir()) {
return pos.up();
}
return this.getDropPos(world, pos.up());
return getDropPos(world, pos.up());
}
protected boolean isPlaceable(BlockState state) {
public boolean isPlaceable(BlockState state) {
if (!state.isOf(this)) return false;
PedestalState currentState = state.get(STATE);
return currentState != PedestalState.BOTTOM &&

View file

@ -9,7 +9,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.util.Tickable;
import net.minecraft.world.World;
import ru.betterend.blocks.basis.PedestalBlock;
import ru.betterend.registry.EndBlockEntities;
import ru.betterend.registry.EndItems;
@ -29,17 +28,11 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
}
public int getAge() {
return this.age;
return age;
}
public int getMaxAge() {
return this.maxAge;
}
@Override
public void clear() {
this.activeItem = ItemStack.EMPTY;
this.markDirty();
return maxAge;
}
@Override
@ -49,42 +42,41 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
@Override
public boolean isEmpty() {
return this.activeItem.isEmpty();
return activeItem.isEmpty();
}
@Override
public ItemStack getStack(int slot) {
return this.activeItem;
return activeItem;
}
@Override
public ItemStack removeStack(int slot, int amount) {
return this.removeStack(slot);
return removeStack(slot);
}
@Override
public boolean isValid(int slot, ItemStack stack) {
return this.isEmpty();
return isEmpty();
}
@Override
public void clear() {
activeItem = ItemStack.EMPTY;
markDirty();
}
@Override
public ItemStack removeStack(int slot) {
ItemStack stored = this.activeItem;
this.activeItem = ItemStack.EMPTY;
this.markDirty();
ItemStack stored = activeItem;
clear();
return stored;
}
public void removeStack(World world, BlockState state) {
world.setBlockState(pos, state.with(PedestalBlock.HAS_ITEM, false)
.with(PedestalBlock.HAS_LIGHT, false));
this.removeStack(0);
}
@Override
public void setStack(int slot, ItemStack stack) {
this.activeItem = stack;
this.markDirty();
activeItem = stack;
markDirty();
}
@Override
@ -92,17 +84,18 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
if (world != null && !world.isClient) {
BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof PedestalBlock) {
state = state.with(PedestalBlock.HAS_ITEM, !isEmpty());
BlockState trueState = state.with(PedestalBlock.HAS_ITEM, !isEmpty());
if (activeItem.getItem() == EndItems.ETERNAL_CRYSTAL) {
state = state.with(PedestalBlock.HAS_LIGHT, true);
trueState = trueState.with(PedestalBlock.HAS_LIGHT, true);
} else {
state = state.with(PedestalBlock.HAS_LIGHT, false);
trueState = trueState.with(PedestalBlock.HAS_LIGHT, false);
}
world.setBlockState(pos, state);
world.setBlockState(pos, trueState);
}
}
super.markDirty();
}
@Override
public boolean canPlayerUse(PlayerEntity player) {
@ -111,12 +104,12 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
@Override
public BlockEntityUpdateS2CPacket toUpdatePacket() {
return new BlockEntityUpdateS2CPacket(pos, 32, this.toInitialChunkDataTag());
return new BlockEntityUpdateS2CPacket(pos, 32, toInitialChunkDataTag());
}
@Override
public CompoundTag toInitialChunkDataTag() {
return this.toTag(new CompoundTag());
return toTag(new CompoundTag());
}
@Override
@ -124,7 +117,7 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
super.fromTag(state, tag);
if (tag.contains("active_item")) {
CompoundTag itemTag = tag.getCompound("active_item");
this.activeItem = ItemStack.fromTag(itemTag);
activeItem = ItemStack.fromTag(itemTag);
}
}
@ -138,9 +131,9 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
@Override
public void tick() {
if (!isEmpty()) {
this.age++;
age++;
if (age > maxAge) {
this.age = 0;
age = 0;
}
}
}

View file

@ -15,6 +15,7 @@ import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import ru.betterend.blocks.EternalPedestal;
import ru.betterend.blocks.basis.PedestalBlock;
import ru.betterend.blocks.entities.PedestalBlockEntity;
@ -34,16 +35,17 @@ public class PedestalItemRenderer<T extends PedestalBlockEntity> extends BlockEn
@Override
public void render(T blockEntity, float tickDelta, MatrixStack matrices,
VertexConsumerProvider vertexConsumers, int light, int overlay) {
if (blockEntity.isEmpty()) return;
BlockState state = blockEntity.getWorld().getBlockState(blockEntity.getPos());
World world = blockEntity.getWorld();
if (blockEntity.isEmpty() || world == null) return;
BlockState state = world.getBlockState(blockEntity.getPos());
if (!(state.getBlock() instanceof PedestalBlock)) return;
ItemStack activeItem = blockEntity.getStack(0);
matrices.push();
MinecraftClient minecraft = MinecraftClient.getInstance();
BakedModel model = minecraft.getItemRenderer().getHeldItemModel(activeItem, blockEntity.getWorld(), null);
BakedModel model = minecraft.getItemRenderer().getHeldItemModel(activeItem, world, null);
Vector3f translate = model.getTransformation().ground.translation;
PedestalBlock pedestal = (PedestalBlock) state.getBlock();
matrices.translate(translate.getX(), translate.getY(), translate.getZ());