added drill
Some checks are pending
build / build (21) (push) Waiting to run

This commit is contained in:
Jottyfan 2024-11-30 00:31:23 +01:00
parent c5c4c4d8a6
commit f59ceb8883
27 changed files with 653 additions and 0 deletions

View File

@ -0,0 +1,111 @@
package de.jottyfan.quickiemod.block;
import java.util.HashMap;
import java.util.Map;
import com.mojang.serialization.MapCodec;
import de.jottyfan.quickiemod.blockentity.DrillBlockEntity;
import de.jottyfan.quickiemod.blockentity.EnumDrillDirection;
import de.jottyfan.quickiemod.item.ModItems;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.FallingBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.state.StateManager.Builder;
import net.minecraft.state.property.IntProperty;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
/**
*
* @author jotty
*
*/
public class BlockDrill extends FallingBlock implements BlockEntityProvider {
private static final Integer MAX_FUEL = 255;
public static final IntProperty FUEL = IntProperty.of("fuel", 0, MAX_FUEL);
public static final IntProperty DIRECTION = IntProperty.of("direction", 0, 4);
public BlockDrill(Identifier identifier, EnumDrillDirection direction) {
super(AbstractBlock.Settings.create().hardness(2.5f).registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
setDefaultState(getDefaultState().with(FUEL, 0).with(DIRECTION, direction.get()));
}
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState blockState) {
return new DrillBlockEntity(pos, blockState);
}
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state,
BlockEntityType<T> type) {
return (world1, pos, state1, be) -> DrillBlockEntity.tick(world1, pos, state1, be);
}
@Override
protected MapCodec<? extends FallingBlock> getCodec() {
// TODO Auto-generated method stub
return null;
}
@Override
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
Integer fuelLeft = state.get(FUEL);
world.spawnEntity(
new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(ModItems.ITEM_CANOLABOTTLE, fuelLeft)));
return super.onBreak(world, pos, state, player);
}
@Override
protected void appendProperties(Builder<Block, BlockState> builder) {
builder.add(FUEL).add(DIRECTION);
}
@Override
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
Map<Item, Integer> loadings = new HashMap<>();
loadings.put(ModItems.ITEM_CANOLABOTTLE, 8);
loadings.put(ModItems.ITEM_CANOLABOTTLESTACK, 72);
ItemStack stack = player.getStackInHand(player.getActiveHand());
Item item = stack.getItem();
if (stack.isEmpty() || !loadings.containsKey(item)) {
if (world.isClient()) {
player.sendMessage(Text.translatable("msg.drill.fuelinfo", state.get(FUEL)), false);
}
} else {
Integer fuelWeight = loadings.get(item);
if (fuelWeight != null) {
Integer load = MAX_FUEL - state.get(FUEL);
if (load < fuelWeight) {
fuelWeight = load;
}
world.setBlockState(pos, state.with(FUEL, state.get(FUEL) + fuelWeight));
if (item.equals(ModItems.ITEM_CANOLABOTTLE)) {
world.spawnEntity(
new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.GLASS_BOTTLE, 1)));
} else if (item.equals(ModItems.ITEM_CANOLABOTTLESTACK)) {
world.spawnEntity(
new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.GLASS_BOTTLE, 9)));
}
stack.decrement(1);
}
}
return ActionResult.PASS;
}
}

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import de.jottyfan.quickiemod.Quickiemod;
import de.jottyfan.quickiemod.blockentity.EnumDrillDirection;
import de.jottyfan.quickiemod.identifier.ModIdentifiers;
import de.jottyfan.quickiemod.item.ModItems;
import net.minecraft.block.Block;
@ -60,6 +61,16 @@ public class ModBlocks {
new BlockPlant(ModIdentifiers.BLOCK_COTTONPLANT, ModItems.ITEM_COTTONSEED, ModItems.ITEM_COTTON), false);
public static final Block BLOCK_CANOLAPLANT = registerBlock(ModIdentifiers.BLOCK_CANOLAPLANT,
new BlockPlant(ModIdentifiers.BLOCK_CANOLAPLANT, ModItems.ITEM_CANOLASEED, ModItems.ITEM_CANOLA), false);
public static final Block BLOCK_DRILL_DOWN = registerBlock(ModIdentifiers.BLOCK_DRILLDOWN,
new BlockDrill(ModIdentifiers.BLOCK_DRILLDOWN, EnumDrillDirection.DOWN));
public static final Block BLOCK_DRILL_EAST = registerBlock(ModIdentifiers.BLOCK_DRILLEAST,
new BlockDrill(ModIdentifiers.BLOCK_DRILLEAST, EnumDrillDirection.EAST));
public static final Block BLOCK_DRILL_SOUTH = registerBlock(ModIdentifiers.BLOCK_DRILLSOUTH,
new BlockDrill(ModIdentifiers.BLOCK_DRILLSOUTH, EnumDrillDirection.SOUTH));
public static final Block BLOCK_DRILL_WEST = registerBlock(ModIdentifiers.BLOCK_DRILLWEST,
new BlockDrill(ModIdentifiers.BLOCK_DRILLWEST, EnumDrillDirection.WEST));
public static final Block BLOCK_DRILL_NORTH = registerBlock(ModIdentifiers.BLOCK_DRILLNORTH,
new BlockDrill(ModIdentifiers.BLOCK_DRILLNORTH, EnumDrillDirection.NORTH));
private static final Block registerBlock(Identifier identifier, Block block) {
return registerBlock(identifier, block, true);
@ -93,6 +104,11 @@ public class ModBlocks {
blocks.add(BLOCK_ORESULFOR);
blocks.add(BLOCK_SANDSALPETER);
blocks.add(BLOCK_KELPSTACK);
blocks.add(BLOCK_DRILL_DOWN);
blocks.add(BLOCK_DRILL_EAST);
blocks.add(BLOCK_DRILL_SOUTH);
blocks.add(BLOCK_DRILL_WEST);
blocks.add(BLOCK_DRILL_NORTH);
return blocks;
}
}

View File

@ -0,0 +1,142 @@
package de.jottyfan.quickiemod.blockentity;
import java.util.ArrayList;
import java.util.List;
import de.jottyfan.quickiemod.block.BlockDrill;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
/**
*
* @author jotty
*
*/
public class DrillBlockEntity extends BlockEntity {
private static final Integer MAXDRILLSTEP = 20;
private Integer drillstep;
private final Integer maxDrillStep;
public DrillBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockentity.DRILL_BLOCKENTITY, pos, state);
this.maxDrillStep = MAXDRILLSTEP;
}
public static void tick(World world, BlockPos pos, BlockState state, BlockEntity be) {
if (be instanceof DrillBlockEntity dbe) {
Integer dir = state.get(BlockDrill.DIRECTION);
List<BlockPos> list = new ArrayList<>();
if (EnumDrillDirection.DOWN.get().equals(dir)) {
list = downFrom(pos);
} else if (EnumDrillDirection.EAST.get().equals(dir)) {
list = directedFrom(pos.east());
} else if (EnumDrillDirection.SOUTH.get().equals(dir)) {
list = directedFrom(pos.south());
} else if (EnumDrillDirection.WEST.get().equals(dir)) {
list = directedFrom(pos.west());
} else if (EnumDrillDirection.NORTH.get().equals(dir)) {
list = directedFrom(pos.north());
}
DrillBlockEntity.tick(world, pos, state, dbe, MAXDRILLSTEP, list);
}
}
public static final List<BlockPos> directedFrom(BlockPos pos) {
// Quickiemod.LOGGER.info("directed");
List<BlockPos> list = new ArrayList<>();
list.add(pos);
list.add(pos.up());
list.add(pos.up().up());
list.add(pos.up().up().up());
list.add(pos.down()); // must be last position
return list;
}
public static final List<BlockPos> downFrom(BlockPos pos) {
// Quickiemod.LOGGER.info("down");
List<BlockPos> list = new ArrayList<>();
Integer tracesMod = pos.getY() % 8;
tracesMod = tracesMod < 0 ? tracesMod * -1 : tracesMod; // lower that 0 makes it negative
if (tracesMod != 0) {
list.add(pos.north());
}
if (tracesMod != 1) {
list.add(pos.north().west());
}
if (tracesMod != 2) {
list.add(pos.west());
}
if (tracesMod != 3) {
list.add(pos.south().west());
}
if (tracesMod != 4) {
list.add(pos.south());
}
if (tracesMod != 5) {
list.add(pos.south().east());
}
if (tracesMod != 6) {
list.add(pos.east());
}
if (tracesMod != 7) {
list.add(pos.north().east());
}
list.add(pos.down()); // must be last position
return list;
}
protected static final void moveBlockWithEntity(BlockPos from, BlockPos to, World world) {
BlockEntity be = world.getBlockEntity(from);
BlockState bs = world.getBlockState(from);
if (be != null) {
world.setBlockState(from, Blocks.AIR.getDefaultState());
Integer newFuel = bs.get(BlockDrill.FUEL) - 1;
world.setBlockState(to, bs.with(BlockDrill.FUEL, newFuel));
world.removeBlockEntity(from);
}
}
protected static final Boolean drill(BlockPos pos, List<BlockPos> toList, World world) {
Boolean lastSuccess = false;
for (BlockPos to : toList) {
if (!world.getBlockState(to).isOf(Blocks.BEDROCK)) {
world.breakBlock(to, true);
lastSuccess = pos.down() != to; // no need for the falling one
} else {
lastSuccess = false; // in case that the last one is a bedrock
}
}
return lastSuccess;
}
public static void tick(World world, BlockPos pos, BlockState state, DrillBlockEntity be, Integer maxDrillStep,
List<BlockPos> drillPosition) {
if (state.get(BlockDrill.FUEL) > 0) {
if (be.getDrillstep() < 1) {
be.setDrillstep(maxDrillStep);
if (drill(pos, drillPosition, world)) {
moveBlockWithEntity(pos, drillPosition.get(drillPosition.size() - 1), world);
}
} else {
be.doDrillstep();
}
}
}
public void doDrillstep() {
setDrillstep(getDrillstep() - 1);
}
public Integer getDrillstep() {
return drillstep == null ? maxDrillStep : drillstep;
}
public void setDrillstep(Integer drillstep) {
this.drillstep = drillstep;
}
}

View File

@ -0,0 +1,36 @@
package de.jottyfan.quickiemod.blockentity;
/**
*
* @author jotty
*
*/
public enum EnumDrillDirection {
DOWN(0), EAST(1), SOUTH(2), WEST(3), NORTH(4);
private final Integer value;
private EnumDrillDirection(Integer value) {
this.value = value;
}
@Override
public String toString() {
return String.format("%s: %d", EnumDrillDirection.lookupValue(value), value);
}
public static final String lookupValue(Integer value) {
return switch (value) {
case 0 -> "down";
case 1 -> "east";
case 2 -> "south";
case 3 -> "west";
case 4 -> "north";
default -> "not supported";
};
}
public Integer get() {
return value;
}
}

View File

@ -16,6 +16,10 @@ public class ModBlockentity {
public static final BlockEntityType<ItemHoarderBlockEntity> ITEM_HOARDER_BLOCKENTITY = Registry.register(
Registries.BLOCK_ENTITY_TYPE, ModIdentifiers.BLOCKENTITY_ITEMHOARDER,
FabricBlockEntityTypeBuilder.create(ItemHoarderBlockEntity::new, ModBlocks.BLOCK_ITEMHOARDER).build());
public static final BlockEntityType<DrillBlockEntity> DRILL_BLOCKENTITY = Registry.register(
Registries.BLOCK_ENTITY_TYPE, ModIdentifiers.BLOCKENTITY_DRILL,
FabricBlockEntityTypeBuilder.create(DrillBlockEntity::new, ModBlocks.BLOCK_DRILL_DOWN, ModBlocks.BLOCK_DRILL_EAST,
ModBlocks.BLOCK_DRILL_SOUTH, ModBlocks.BLOCK_DRILL_WEST, ModBlocks.BLOCK_DRILL_NORTH).build());
public static final void registerModBlockentities() {
};

View File

@ -58,7 +58,13 @@ public class ModIdentifiers {
public static final Identifier BLOCK_KELPSTACK = Identifier.of(Quickiemod.MOD_ID, "kelpstack");
public static final Identifier BLOCK_COTTONPLANT = Identifier.of(Quickiemod.MOD_ID, "blockcottonplant");
public static final Identifier BLOCK_CANOLAPLANT = Identifier.of(Quickiemod.MOD_ID, "blockcanolaplant");
public static final Identifier BLOCK_DRILLDOWN = Identifier.of(Quickiemod.MOD_ID, "drill");
public static final Identifier BLOCK_DRILLEAST = Identifier.of(Quickiemod.MOD_ID, "drilleast");
public static final Identifier BLOCK_DRILLSOUTH = Identifier.of(Quickiemod.MOD_ID, "drillsouth");
public static final Identifier BLOCK_DRILLWEST = Identifier.of(Quickiemod.MOD_ID, "drillwest");
public static final Identifier BLOCK_DRILLNORTH = Identifier.of(Quickiemod.MOD_ID, "drillnorth");
public static final Identifier BLOCKENTITY_ITEMHOARDER = Identifier.of(Quickiemod.MOD_ID, "itemhoarderblockentity");
public static final Identifier BLOCKENTITY_BLOCKSTACKER = Identifier.of(Quickiemod.MOD_ID, "blockstackerblockentity");
public static final Identifier BLOCKENTITY_DRILL = Identifier.of(Quickiemod.MOD_ID, "drillblockentity");
}

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "quickiemod:block/drill"
}
}
}

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "quickiemod:block/drilleast"
}
}
}

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "quickiemod:block/drillnorth"
}
}
}

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "quickiemod:block/drillsouth"
}
}
}

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "quickiemod:block/drillwest"
}
}
}

View File

@ -0,0 +1,6 @@
{
"parent": "block/cube_all",
"textures": {
"all": "quickiemod:block/drill"
}
}

View File

@ -0,0 +1,45 @@
{
"parent": "block/cube",
"textures": {
"bottom": "quickiemod:block/drilleast",
"all": "quickiemod:block/drill"
},
"elements": [
{
"from": [0, 0, 0],
"to": [16, 16, 16],
"faces": {
"down": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "down"
},
"up": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "up"
},
"north": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "north"
},
"south": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "south"
},
"east": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "east"
},
"west": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "west"
}
}
}
]
}

View File

@ -0,0 +1,45 @@
{
"parent": "block/cube",
"textures": {
"bottom": "quickiemod:block/drillnorth",
"all": "quickiemod:block/drill"
},
"elements": [
{
"from": [0, 0, 0],
"to": [16, 16, 16],
"faces": {
"down": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "down"
},
"up": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "up"
},
"north": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "north"
},
"south": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "south"
},
"east": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "east"
},
"west": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "west"
}
}
}
]
}

View File

@ -0,0 +1,45 @@
{
"parent": "block/cube",
"textures": {
"bottom": "quickiemod:block/drillsouth",
"all": "quickiemod:block/drill"
},
"elements": [
{
"from": [0, 0, 0],
"to": [16, 16, 16],
"faces": {
"down": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "down"
},
"up": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "up"
},
"north": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "north"
},
"south": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "south"
},
"east": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "east"
},
"west": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "west"
}
}
}
]
}

View File

@ -0,0 +1,45 @@
{
"parent": "block/cube",
"textures": {
"bottom": "quickiemod:block/drillwest",
"all": "quickiemod:block/drill"
},
"elements": [
{
"from": [0, 0, 0],
"to": [16, 16, 16],
"faces": {
"down": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "down"
},
"up": {
"uv": [0, 0, 16, 16],
"texture": "#bottom",
"cullface": "up"
},
"north": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "north"
},
"south": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "south"
},
"east": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "east"
},
"west": {
"uv": [0, 0, 16, 16],
"texture": "#all",
"cullface": "west"
}
}
}
]
}

View File

@ -0,0 +1,10 @@
{
"parent": "quickiemod:block/drill",
"display": {
"thirdperson": {
"rotation": [ 10, -45, 170 ],
"translation": [ 0, 1.5, -2.75 ],
"scale": [ 0.375, 0.375, 0.375 ]
}
}
}

View File

@ -0,0 +1,10 @@
{
"parent": "quickiemod:block/drilleast",
"display": {
"thirdperson": {
"rotation": [ 10, -45, 170 ],
"translation": [ 0, 1.5, -2.75 ],
"scale": [ 0.375, 0.375, 0.375 ]
}
}
}

View File

@ -0,0 +1,10 @@
{
"parent": "quickiemod:block/drillnorth",
"display": {
"thirdperson": {
"rotation": [ 10, -45, 170 ],
"translation": [ 0, 1.5, -2.75 ],
"scale": [ 0.375, 0.375, 0.375 ]
}
}
}

View File

@ -0,0 +1,10 @@
{
"parent": "quickiemod:block/drillsouth",
"display": {
"thirdperson": {
"rotation": [ 10, -45, 170 ],
"translation": [ 0, 1.5, -2.75 ],
"scale": [ 0.375, 0.375, 0.375 ]
}
}
}

View File

@ -0,0 +1,10 @@
{
"parent": "quickiemod:block/drillwest",
"display": {
"thirdperson": {
"rotation": [ 10, -45, 170 ],
"translation": [ 0, 1.5, -2.75 ],
"scale": [ 0.375, 0.375, 0.375 ]
}
}
}

View File

@ -0,0 +1,17 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
"qbq",
"dqd",
" d "
],
"key": {
"q": "quickiemod:quickieingot",
"d": "minecraft:diamond",
"b": "minecraft:barrel"
},
"result": {
"id": "quickiemod:drill",
"count": 1
}
}

View File

@ -0,0 +1,10 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
"quickiemod:drill"
],
"result": {
"id": "quickiemod:drilleast",
"count": 1
}
}

View File

@ -0,0 +1,10 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
"quickiemod:drilleast"
],
"result": {
"id": "quickiemod:drillsouth",
"count": 1
}
}

View File

@ -0,0 +1,10 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
"quickiemod:drillnorth"
],
"result": {
"id": "quickiemod:drill",
"count": 1
}
}

View File

@ -0,0 +1,10 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
"quickiemod:drillsouth"
],
"result": {
"id": "quickiemod:drillwest",
"count": 1
}
}

View File

@ -0,0 +1,10 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
"quickiemod:drillwest"
],
"result": {
"id": "quickiemod:drillnorth",
"count": 1
}
}