From c3d602202f43da9bf9631f29495f3e08874f8576 Mon Sep 17 00:00:00 2001 From: DeltaHelios Date: Sun, 21 Apr 2024 09:14:36 +0300 Subject: [PATCH] Plate block start --- .../com/parzivail/pswg/block/PlateBlock.java | 79 +++++++++++++++++++ .../pswg/block/PlateBlockEntity.java | 59 ++++++++++++++ .../parzivail/pswg/container/SwgBlocks.java | 8 ++ 3 files changed, 146 insertions(+) create mode 100644 projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlock.java create mode 100644 projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlockEntity.java diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlock.java b/projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlock.java new file mode 100644 index 000000000..799e2e7d5 --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlock.java @@ -0,0 +1,79 @@ +package com.parzivail.pswg.block; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.BlockWithEntity; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.state.StateManager; +import net.minecraft.state.property.IntProperty; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; + +public class PlateBlock extends BlockWithEntity +{ + public final static IntProperty FOOD_AMOUNT = IntProperty.of("food_amount", 0, 5); + + public PlateBlock(Settings settings) + { + super(settings); + } + + @Nullable + @Override + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) + { + return new PlateBlockEntity(pos, state); + } + + @Override + protected void appendProperties(StateManager.Builder builder) + { + super.appendProperties(builder.add(FOOD_AMOUNT)); + } + + private void addFood(World world, BlockPos pos, BlockState state, ItemStack stack) + { + BlockEntity blockEntity = world.getBlockEntity(pos); + int foodAmount = state.get(FOOD_AMOUNT); + if (stack.isFood() && blockEntity instanceof PlateBlockEntity plateBlockEntity && foodAmount < 5) + plateBlockEntity.addFood(stack); + } + + private void takeFood(World world, BlockPos pos, BlockState state, PlayerEntity player) + { + if (state.get(FOOD_AMOUNT) > 0) + { + var blockEntity = world.getBlockEntity(pos); + if (blockEntity instanceof PlateBlockEntity plateBlockEntity) + plateBlockEntity.removeFood(player); + } + } + + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) + { + var stack = player.getStackInHand(hand); + if (player.isSneaking() && state.get(FOOD_AMOUNT) > 0) + { + takeFood(world, pos, state, player); + world.setBlockState(pos, state.with(FOOD_AMOUNT, state.get(FOOD_AMOUNT) - 1)); + return ActionResult.SUCCESS; + } + else if (!stack.isEmpty() && state.get(FOOD_AMOUNT) < 5) + { + var newStack = new ItemStack(stack.getItem(), 1); + addFood(world, pos, state, newStack); + if (player.isCreative()) + stack.decrement(1); + world.setBlockState(pos, state.with(FOOD_AMOUNT, state.get(FOOD_AMOUNT) + 1)); + return ActionResult.SUCCESS; + } + return ActionResult.PASS; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlockEntity.java b/projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlockEntity.java new file mode 100644 index 000000000..46b837e4f --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlockEntity.java @@ -0,0 +1,59 @@ +package com.parzivail.pswg.block; + +import com.parzivail.pswg.container.SwgBlocks; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.util.math.BlockPos; + +import java.util.ArrayList; +import java.util.List; + +public class PlateBlockEntity extends BlockEntity +{ + public static int FOOD_AMOUNT; + public List FOODS = new ArrayList<>(); + + public PlateBlockEntity(BlockPos pos, BlockState state) + { + super(SwgBlocks.Misc.PlateBlockEntityType, pos, state); + FOOD_AMOUNT = 0; + } + + @Override + protected void writeNbt(NbtCompound nbt) + { + nbt.putInt("food_amount", FOODS.size()); + for (int i = 0; i < FOODS.size(); i++) + { + nbt.put("Food" + i, this.FOODS.get(i).getNbt()); + } + super.writeNbt(nbt); + } + + @Override + public void readNbt(NbtCompound nbt) + { + super.readNbt(nbt); + FOOD_AMOUNT = nbt.getInt("food_amount"); + for (int i = 0; i < FOOD_AMOUNT; i++) + { + this.FOODS.set(i, ItemStack.fromNbt(nbt.getCompound("Food" + i))); + } + } + + public void addFood(ItemStack stack) + { + FOODS.add(stack); + FOOD_AMOUNT++; + } + + public void removeFood(PlayerEntity player) + { + player.giveItemStack(FOODS.get(FOODS.size() - 1)); + FOODS.remove(FOODS.size() - 1); + FOOD_AMOUNT--; + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgBlocks.java b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgBlocks.java index 98e90cf08..61cd7c854 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgBlocks.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/container/SwgBlocks.java @@ -964,6 +964,14 @@ public static class Misc @ClientBlockRegistryData(renderLayer = RenderLayerHint.CUTOUT_MIPPED) @TarkinBlock(state = TrState.None, model = TrModel.None, loot = TrLoot.Pickling) public static final ThermalDetonatorBlock ThermalDetonatorBlock = new ThermalDetonatorBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).nonOpaque().strength(0.5F)); + + @RegistryName("plate") + @ClientBlockRegistryData(renderLayer = RenderLayerHint.CUTOUT_MIPPED) + @TarkinBlock(state = TrState.None, model = TrModel.None) + public static final Block Plate = new PlateBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).strength(1F)); + + @RegistryName("plate") + public static final BlockEntityType PlateBlockEntityType = FabricBlockEntityTypeBuilder.create(PlateBlockEntity::new, Plate).build(); } public static void register()