Add the insolator #17

Merged
BlakeRain merged 19 commits from BlakeRain/utamacraft:main into main 2023-12-01 18:20:57 +00:00
11 changed files with 109 additions and 24 deletions
Showing only changes of commit da4b282659 - Show all commits

View File

@ -9,6 +9,7 @@ import net.banutama.utamacraft.item.ModItems;
import net.banutama.utamacraft.screen.InsolatorScreen;
import net.banutama.utamacraft.screen.ModMenuTypes;
import net.banutama.utamacraft.sound.ModSounds;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
@ -36,6 +37,7 @@ public class Utamacraft {
ModBlockEntities.register(bus);
CCRegistration.register(bus);
ModMenuTypes.register(bus);
ModSounds.register(bus);
bus.addListener(this::commonSetup);
MinecraftForge.EVENT_BUS.register(this);

View File

@ -1,17 +1,18 @@
package net.banutama.utamacraft.block.custom;
import net.banutama.utamacraft.Utamacraft;
import net.banutama.utamacraft.block.entity.InsolatorBlockEntity;
import net.banutama.utamacraft.block.entity.ModBlockEntities;
import net.banutama.utamacraft.item.ModCreativeModeTab;
import net.banutama.utamacraft.item.ModItems;
import net.banutama.utamacraft.sound.ModSounds;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
@ -22,31 +23,33 @@ import net.minecraft.world.level.block.entity.BlockEntityType;
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.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.Nullable;
import java.util.function.Supplier;
public class InsolatorBlock extends BaseEntityBlock {
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
public static final BooleanProperty ACTIVE = BooleanProperty.create("active");
private static VoxelShape SHAPE =
Block.box(0, 0, 0, 16, 16, 16);
public InsolatorBlock() {
super(getProperties());
registerDefaultState(this.getStateDefinition().any().setValue(FACING, Direction.NORTH).setValue(ACTIVE, false));
}
private static Block.Properties getProperties() {
return Block.Properties.of(Material.METAL).strength(6.0f).requiresCorrectToolForDrops().noOcclusion();
return Block.Properties.of(Material.METAL)
.strength(6.0f)
.requiresCorrectToolForDrops()
.noOcclusion()
.lightLevel(state -> state.getValue(ACTIVE) ? 15 : 0);
}
@Override
@ -72,7 +75,7 @@ public class InsolatorBlock extends BaseEntityBlock {
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING);
builder.add(FACING, ACTIVE);
}
@Override
@ -82,10 +85,14 @@ public class InsolatorBlock extends BaseEntityBlock {
@Override
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
if (pState.getBlock() != pNewState.getBlock()) {
if (!pState.is(pNewState.getBlock())) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
if (blockEntity instanceof InsolatorBlockEntity insolatorEntity) {
insolatorEntity.drops();
if (blockEntity instanceof InsolatorBlockEntity insolator) {
if (pLevel instanceof ServerLevel) {
insolator.drops();
}
pLevel.updateNeighbourForOutputSignal(pPos, this);
}
}
@ -115,6 +122,17 @@ public class InsolatorBlock extends BaseEntityBlock {
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
return createTickerHelper(pBlockEntityType, ModBlockEntities.INSOLATOR.get(), InsolatorBlockEntity::tick);
return pLevel.isClientSide ? null : createTickerHelper(pBlockEntityType, ModBlockEntities.INSOLATOR.get(), InsolatorBlockEntity::serverTick);
}
@Override
public void animateTick(BlockState pState, Level pLevel, BlockPos pPos, RandomSource pRandom) {
// if (pState.getValue(ACTIVE)) {
// if (pRandom.nextInt(100) == 0) {
// pLevel.playLocalSound(
// pPos.getX() + 0.5, pPos.getY() + 0.5, pPos.getZ() + 0.5,
// ModSounds.INSOLATOR.get(), SoundSource.BLOCKS, 0.5f, pRandom.nextFloat() * 0.4F + 0.8F, false);
// }
// }
}
}

View File

@ -1,6 +1,7 @@
package net.banutama.utamacraft.block.entity;
import com.mojang.logging.LogUtils;
import net.banutama.utamacraft.block.custom.InsolatorBlock;
import net.banutama.utamacraft.screen.InsolatorMenu;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
@ -41,6 +42,7 @@ public class InsolatorBlockEntity extends BlockEntity implements MenuProvider {
protected final ContainerData data;
private int progress = 0;
private int maxProgress = 60;
private boolean active = false;
public InsolatorBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(ModBlockEntities.INSOLATOR.get(), pPos, pBlockState);
@ -50,6 +52,7 @@ public class InsolatorBlockEntity extends BlockEntity implements MenuProvider {
return switch (pIndex) {
case 0 -> InsolatorBlockEntity.this.progress;
case 1 -> InsolatorBlockEntity.this.maxProgress;
case 2 -> InsolatorBlockEntity.this.active ? 1 : 0;
default -> 0;
};
}
@ -59,12 +62,13 @@ public class InsolatorBlockEntity extends BlockEntity implements MenuProvider {
switch (pIndex) {
case 0 -> InsolatorBlockEntity.this.progress = pValue;
case 1 -> InsolatorBlockEntity.this.maxProgress = pValue;
case 2 -> InsolatorBlockEntity.this.active = pValue != 0;
}
}
@Override
public int getCount() {
return 2;
return 3;
}
};
}
@ -128,13 +132,16 @@ public class InsolatorBlockEntity extends BlockEntity implements MenuProvider {
}
}
public static void tick(Level level, BlockPos pos, BlockState state, InsolatorBlockEntity entity) {
public static void serverTick(Level level, BlockPos pos, BlockState state, InsolatorBlockEntity entity) {
if (level.isClientSide()) {
return;
}
boolean newActive;
if (entity.canCraft()) {
++entity.progress;
newActive = true;
setChanged(level, pos, state);
if (entity.progress >= entity.maxProgress) {
@ -142,8 +149,15 @@ public class InsolatorBlockEntity extends BlockEntity implements MenuProvider {
}
} else {
entity.resetProgress();
newActive = false;
setChanged(level, pos, state);
}
if (newActive != entity.active) {
entity.active = newActive;
state = state.setValue(InsolatorBlock.ACTIVE, newActive);
level.setBlock(pos, state, 3);
}
}
private void resetProgress() {

View File

@ -23,7 +23,7 @@ public class InsolatorMenu extends AbstractContainerMenu {
private final ContainerData data;
public InsolatorMenu(int id, Inventory inventory, FriendlyByteBuf extraData) {
this(id, inventory, inventory.player.level.getBlockEntity(extraData.readBlockPos()), new SimpleContainerData(2));
this(id, inventory, inventory.player.level.getBlockEntity(extraData.readBlockPos()), new SimpleContainerData(3));
}
public InsolatorMenu(int id, Inventory inventory, BlockEntity entity, ContainerData data) {

View File

@ -0,0 +1,22 @@
package net.banutama.utamacraft.sound;
import net.banutama.utamacraft.Utamacraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class ModSounds {
public static final DeferredRegister<SoundEvent> SOUNDS =
DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, Utamacraft.MOD_ID);
public static final RegistryObject<SoundEvent> INSOLATOR =
SOUNDS.register("insolator", () -> new SoundEvent(new ResourceLocation(Utamacraft.MOD_ID, "insolator")));
public static void register(IEventBus eventBus) {
SOUNDS.register(eventBus);
}
}

View File

@ -1,19 +1,34 @@
{
"variants": {
"facing=north": {
"facing=north,active=false": {
"model": "utamacraft:block/insolator"
},
"facing=east": {
"facing=north,active=true": {
"model": "utamacraft:block/insolator_active"
},
"facing=east,active=false": {
"model": "utamacraft:block/insolator",
"y": 90
},
"facing=south": {
"facing=east,active=true": {
"model": "utamacraft:block/insolator_active",
"y": 90
},
"facing=south,active=false": {
"model": "utamacraft:block/insolator",
"y": 180
},
"facing=west": {
"facing=south,active=true": {
"model": "utamacraft:block/insolator_active",
"y": 180
},
"facing=west,active=false": {
"model": "utamacraft:block/insolator",
"y": 270
},
"facing=west,active=true": {
"model": "utamacraft:block/insolator_active",
"y": 270
}
}
}

View File

@ -0,0 +1,6 @@
{
"parent": "utamacraft:block/insolator",
"textures": {
"5": "utamacraft:block/insolator_active"
}
}

View File

@ -0,0 +1,8 @@
{
"insolator": {
"category": "block",
"sounds": [
"utamacraft:insolator"
]
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 970 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 974 B