Skip to content

Commit

Permalink
Merge pull request #3 from EngineerSmith/main
Browse files Browse the repository at this point in the history
Feat; Added Barrel and Crate
  • Loading branch information
jasperalani authored Jan 14, 2024
2 parents 3007147 + 4eccbb5 commit e465506
Show file tree
Hide file tree
Showing 51 changed files with 1,325 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'net.minecraftforge.gradle' version '5.1.+'
}

version = '1.3.8+forge-1.19.2'
version = '1.3.11+forge-1.19.2'
group = 'com.kikis.ptdyeplus'
archivesBaseName = 'ptdyeplus'

Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/kikis/ptdyeplus/PtdyePlus.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.kikis.ptdyeplus;

import com.kikis.ptdyeplus.init.BlockEntityInit;
import com.kikis.ptdyeplus.init.BlockInit;
import com.kikis.ptdyeplus.init.ItemInit;
import com.mojang.logging.LogUtils;
import com.kikis.ptdyeplus.stonecutter.KeyBinding;
import com.simibubi.create.foundation.gui.ScreenOpener;
import com.simibubi.create.foundation.ponder.PonderRegistry;
Expand All @@ -17,19 +21,29 @@
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.slf4j.Logger;
import java.util.*;


public class PtdyePlus
{
public static final String ID = "ptdyeplus";
public static final Logger LOGGER = LogUtils.getLogger();
private static final Minecraft minecraft = Minecraft.getInstance();

public PtdyePlus()
{
MinecraftForge.EVENT_BUS.register(this);

IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();

ItemInit.ITEMS.register(bus);
BlockInit.BLOCKS.register(bus);
BlockEntityInit.ENTITY_TYPES.register(bus);
}

@Mod.EventBusSubscriber(modid = ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/kikis/ptdyeplus/Shape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kikis.ptdyeplus;

import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;

public class Shape {
public VoxelShape shape;
public Shape() {
this.shape = Shapes.empty();
}

public Shape box(int x0, int y0, int z0, int x1, int y1, int z1) {
float x0f = (float) x0 / 16.0F, y0f = (float) y0 / 16.F, z0f = (float) z0 / 16.F;
float x1f = (float) x1 / 16.0F, y1f = (float) y1 / 16.F, z1f = (float) z1 / 16.F;
this.shape = Shapes.join(shape, Shapes.box(x0f, y0f, z0f, x1f, y1f, z1f), BooleanOp.OR);
return this;
}
}
139 changes: 139 additions & 0 deletions src/main/java/com/kikis/ptdyeplus/block/Barrel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.kikis.ptdyeplus.block;

import com.kikis.ptdyeplus.block.entity.BarrelEntity;
import com.kikis.ptdyeplus.block.property.fullness;
import com.kikis.ptdyeplus.Shape;
import net.minecraft.core.BlockPos;
import net.minecraft.stats.Stats;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;

public class Barrel extends BaseEntityBlock {
public static final IntegerProperty FULLNESS;
public static final IntegerProperty POWER;

private static final VoxelShape hitbox = new Shape()
.box(1,0,1, 15,16,15)
.box(3,0,0, 13,16,16)
.box(0,0,3, 16,16,13)
.shape;

public Barrel(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any()
.setValue(fullness.PROPERTY, fullness.MIN)
.setValue(BlockStateProperties.POWER, 0)
);
}

private void updatePower(BlockState blockState, Level level, BlockPos blockPos) {
if (!level.isClientSide) {
int power = level.getBestNeighborSignal(blockPos);
level.setBlock(blockPos, blockState.setValue(POWER, Math.max(0, Math.min(power, 15))), 2);
}
}

@Override
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (level.isClientSide)
return InteractionResult.SUCCESS;
BlockEntity entity = level.getBlockEntity(blockPos);
if (entity instanceof BarrelEntity) {
player.openMenu((BarrelEntity) entity);
player.awardStat(Stats.OPEN_BARREL);
}
return InteractionResult.CONSUME;
}

@Override
public void onRemove(BlockState blockState, Level level, BlockPos blockPos, BlockState blockState2, boolean p_60519_) {
if (!blockState.is(blockState2.getBlock())) {
BlockEntity entity = level.getBlockEntity(blockPos);
if (entity instanceof Container container) {
Containers.dropContents(level, blockPos, container);
level.updateNeighbourForOutputSignal(blockPos, this);
}

super.onRemove(blockState, level, blockPos, blockState, p_60519_);
}
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return new BarrelEntity(blockPos, blockState);
}

@Override
public RenderShape getRenderShape(BlockState p_49232_) {
return RenderShape.MODEL;
}

@Override
public VoxelShape getShape(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos, CollisionContext collisionContext) {
return Barrel.hitbox;
}

@Override
public void setPlacedBy(Level level, BlockPos blockPos, BlockState blockState, @Nullable LivingEntity livingEntity, ItemStack itemStack) {
if (itemStack.hasCustomHoverName()) {
BlockEntity entity = level.getBlockEntity(blockPos);
if (entity instanceof BarrelEntity barrelEntity)
barrelEntity.setCustomName(itemStack.getHoverName());
}
this.updatePower(blockState, level, blockPos);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) {
stateBuilder.add(FULLNESS).add(POWER);
}

@Override
public void neighborChanged(BlockState blockState, Level level, BlockPos blockPos, Block neighborBlock, BlockPos fromPos, boolean moving) {
super.neighborChanged(blockState, level, blockPos, neighborBlock, fromPos, moving);
this.updatePower(blockState, level, blockPos);
}

@Override
public boolean hasAnalogOutputSignal(BlockState p_49058_) {
return true;
}

@Override
public int getAnalogOutputSignal(BlockState blockState, Level level, BlockPos blockPos) {
// edge case that 'should' never happen
if (!(level.getBlockEntity(blockPos) instanceof BarrelEntity entity))
return 0;

int power = blockState.getValue(POWER);
if (power == 0)
return Math.min(entity.getItemCount(null), 15);
return Math.min(entity.getItemCount(entity.getItem(power - 1)), 15);
}

static {
FULLNESS = fullness.PROPERTY;
POWER = BlockStateProperties.POWER;
}
}
140 changes: 140 additions & 0 deletions src/main/java/com/kikis/ptdyeplus/block/Crate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.kikis.ptdyeplus.block;

import com.kikis.ptdyeplus.block.entity.CrateEntity;
import com.kikis.ptdyeplus.block.property.fullness;
import com.kikis.ptdyeplus.Shape;
import net.minecraft.core.BlockPos;
import net.minecraft.stats.Stats;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;

public class Crate extends BaseEntityBlock {
public static final IntegerProperty FULLNESS;
public static final IntegerProperty POWER;


private static final VoxelShape hitbox = new Shape()
.box(0,0,0,16,2,16)
.box(0,14,0, 16,16,16)
.box(1,0,1, 15,16,15)
.shape;

public Crate(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any()
.setValue(FULLNESS, fullness.MIN)
.setValue(POWER, 0)
);
}

private void updatePower(BlockState blockState, Level level, BlockPos blockPos) {
if (!level.isClientSide) {
int power = level.getBestNeighborSignal(blockPos);
level.setBlock(blockPos, blockState.setValue(POWER, Math.max(0, Math.min(power, 15))), 2);
}
}

@Override
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (level.isClientSide)
return InteractionResult.SUCCESS;
BlockEntity entity = level.getBlockEntity(blockPos);
if (entity instanceof CrateEntity) {
player.openMenu((CrateEntity) entity);
player.awardStat(Stats.OPEN_BARREL);
}
return InteractionResult.CONSUME;
}

@Override
public void onRemove(BlockState blockState, Level level, BlockPos blockPos, BlockState blockState2, boolean p_60519_) {
if (!blockState.is(blockState2.getBlock())) {
BlockEntity entity = level.getBlockEntity(blockPos);
if (entity instanceof Container container) {
Containers.dropContents(level, blockPos, container);
level.updateNeighbourForOutputSignal(blockPos, this);
}

super.onRemove(blockState, level, blockPos, blockState, p_60519_);
}
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return new CrateEntity(blockPos, blockState);
}

@Override
public RenderShape getRenderShape(BlockState p_49232_) {
return RenderShape.MODEL;
}

@Override
public VoxelShape getShape(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos, CollisionContext collisionContext) {
return Crate.hitbox;
}

@Override
public void setPlacedBy(Level level, BlockPos blockPos, BlockState blockState, @Nullable LivingEntity livingEntity, ItemStack itemStack) {
if (itemStack.hasCustomHoverName()) {
BlockEntity entity = level.getBlockEntity(blockPos);
if (entity instanceof CrateEntity crateEntity)
crateEntity.setCustomName(itemStack.getHoverName());
}
this.updatePower(blockState, level, blockPos);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) {
stateBuilder.add(FULLNESS).add(POWER);
}

@Override
public void neighborChanged(BlockState blockState, Level level, BlockPos blockPos, Block neighborBlock, BlockPos fromPos, boolean moving) {
super.neighborChanged(blockState, level, blockPos, neighborBlock, fromPos, moving);
this.updatePower(blockState, level, blockPos);
}

@Override
public boolean hasAnalogOutputSignal(BlockState p_49058_) {
return true;
}

@Override
public int getAnalogOutputSignal(BlockState blockState, Level level, BlockPos blockPos) {
// edge case that 'should' never happen
if (!(level.getBlockEntity(blockPos) instanceof CrateEntity entity))
return 0;

int power = blockState.getValue(POWER);
if (power == 0)
return Math.min(entity.getItemCount(null), 15);
return Math.min(entity.getItemCount(entity.getItem(power - 1)), 15);
}

static {
FULLNESS = fullness.PROPERTY;
POWER = BlockStateProperties.POWER;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/kikis/ptdyeplus/block/entity/BarrelEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kikis.ptdyeplus.block.entity;

import com.kikis.ptdyeplus.init.BlockEntityInit;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;

public class BarrelEntity extends BaseContainerEntity {

public BarrelEntity(BlockPos blockPos, BlockState blockState) {
super(blockPos, blockState, 9, 2, BlockEntityInit.BARREL.get(), "ptdyeplus.container.barrel");
}
}
Loading

0 comments on commit e465506

Please sign in to comment.