Fixed structure features and code style

This commit is contained in:
paulevsGitch 2021-07-10 16:07:44 +03:00
parent d431f2555c
commit 5a9365e2bb
153 changed files with 2304 additions and 2459 deletions

View file

@ -26,25 +26,25 @@ import ru.bclib.registry.BaseBlockEntities;
public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
private NonNullList<ItemStack> inventory;
private int viewerCount;
private BaseBarrelBlockEntity(BlockEntityType<?> type, BlockPos blockPos, BlockState blockState) {
super(type, blockPos, blockState);
this.inventory = NonNullList.withSize(27, ItemStack.EMPTY);
}
public BaseBarrelBlockEntity(BlockPos blockPos, BlockState blockState) {
this(BaseBlockEntities.BARREL, blockPos, blockState);
}
public CompoundTag save(CompoundTag tag) {
super.save(tag);
if (!this.trySaveLootTable(tag)) {
ContainerHelper.saveAllItems(tag, this.inventory);
}
return tag;
}
public void load(CompoundTag tag) {
super.load(tag);
this.inventory = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
@ -52,56 +52,57 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
ContainerHelper.loadAllItems(tag, this.inventory);
}
}
public int getContainerSize() {
return 27;
}
protected NonNullList<ItemStack> getItems() {
return this.inventory;
}
protected void setItems(NonNullList<ItemStack> list) {
this.inventory = list;
}
protected Component getDefaultName() {
return new TranslatableComponent("container.barrel");
}
protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) {
return ChestMenu.threeRows(syncId, playerInventory, this);
}
public void startOpen(Player player) {
if (!player.isSpectator()) {
if (viewerCount < 0) {
viewerCount = 0;
}
++viewerCount;
BlockState blockState = this.getBlockState();
if (!blockState.getValue(BarrelBlock.OPEN)) {
playSound(blockState, SoundEvents.BARREL_OPEN);
setOpen(blockState, true);
}
if (level != null) {
scheduleUpdate();
}
}
}
private void scheduleUpdate() {
level.getBlockTicks().scheduleTick(getBlockPos(), getBlockState().getBlock(), 5);
}
public void tick() {
if (level != null) {
viewerCount = ChestBlockEntity.getOpenCount(level, worldPosition);
if (viewerCount > 0) {
scheduleUpdate();
} else {
}
else {
BlockState blockState = getBlockState();
if (!(blockState.getBlock() instanceof BaseBarrelBlock)) {
setRemoved();
@ -114,27 +115,26 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
}
}
}
public void stopOpen(Player player) {
if (!player.isSpectator()) {
--this.viewerCount;
}
}
private void setOpen(BlockState state, boolean open) {
if (level != null) {
level.setBlock(this.getBlockPos(), state.setValue(BarrelBlock.OPEN, open), 3);
}
}
private void playSound(BlockState blockState, SoundEvent soundEvent) {
if (level != null) {
Vec3i vec3i = blockState.getValue(BarrelBlock.FACING).getNormal();
double d = (double) this.worldPosition.getX() + 0.5D + (double) vec3i.getX() / 2.0D;
double e = (double) this.worldPosition.getY() + 0.5D + (double) vec3i.getY() / 2.0D;
double f = (double) this.worldPosition.getZ() + 0.5D + (double) vec3i.getZ() / 2.0D;
level.playSound(null, d, e, f, soundEvent, SoundSource.BLOCKS, 0.5F,
this.level.random.nextFloat() * 0.1F + 0.9F);
level.playSound(null, d, e, f, soundEvent, SoundSource.BLOCKS, 0.5F, this.level.random.nextFloat() * 0.1F + 0.9F);
}
}
}

View file

@ -15,11 +15,11 @@ public class BaseFurnaceBlockEntity extends AbstractFurnaceBlockEntity {
public BaseFurnaceBlockEntity(BlockPos blockPos, BlockState blockState) {
super(BaseBlockEntities.FURNACE, blockPos, blockState, RecipeType.SMELTING);
}
protected Component getDefaultName() {
return new TranslatableComponent("container.furnace");
}
protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) {
return new FurnaceMenu(syncId, playerInventory, this, this.dataAccess);
}

View file

@ -10,7 +10,7 @@ public class BaseSignBlockEntity extends SignBlockEntity {
public BaseSignBlockEntity(BlockPos blockPos, BlockState blockState) {
super(blockPos, blockState);
}
@Override
public BlockEntityType<?> getType() {
return BaseBlockEntities.SIGN;

View file

@ -1,11 +1,6 @@
package ru.bclib.blockentities;
import java.util.Collections;
import java.util.Set;
import java.util.function.Supplier;
import com.google.common.collect.Sets;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
@ -13,33 +8,36 @@ import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
public class DynamicBlockEntityType<T extends BlockEntity> extends BlockEntityType<T> {
import java.util.Collections;
import java.util.Set;
public class DynamicBlockEntityType<T extends BlockEntity> extends BlockEntityType<T> {
private final Set<Block> validBlocks = Sets.newHashSet();
private final BlockEntitySupplier<? extends T> factory;
public DynamicBlockEntityType(BlockEntitySupplier<? extends T> supplier) {
super(null, Collections.emptySet(), null);
this.factory = supplier;
}
@Override
@Nullable public T create(BlockPos blockPos, BlockState blockState) {
@Nullable
public T create(BlockPos blockPos, BlockState blockState) {
return factory.create(blockPos, blockState);
}
@Override
public boolean isValid(BlockState blockState) {
return validBlocks.contains(blockState.getBlock());
}
public void registerBlock(Block block) {
validBlocks.add(block);
}
@FunctionalInterface
public
interface BlockEntitySupplier<T extends BlockEntity> {
public interface BlockEntitySupplier<T extends BlockEntity> {
T create(BlockPos blockPos, BlockState blockState);
}
}