added lavahoarder

This commit is contained in:
Jottyfan
2024-11-20 21:55:49 +01:00
parent 04f87a977e
commit 451f19ea73
12 changed files with 345 additions and 1 deletions

View File

@ -0,0 +1,135 @@
package de.jottyfan.quickiemod.block;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.ExperienceOrbEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootWorldContext.Builder;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.tick.OrderedTick;
/**
*
* @author jotty
*
*/
public class BlockEmptyLavahoarder extends Block {
public BlockEmptyLavahoarder(Identifier identifier) {
super(AbstractBlock.Settings.create().hardness(2.5f).registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
}
@Override
public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) {
List<ItemStack> list = new ArrayList<>();
list.add(new ItemStack(ModBlocks.BLOCK_EMPTYLAVAHOARDER));
return list;
}
@Override
protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos,
net.minecraft.util.math.random.Random random) {
boolean found = BlockLavahoarder.suckLava(world, pos.north());
found = found || BlockLavahoarder.suckLava(world, pos.south());
found = found || BlockLavahoarder.suckLava(world, pos.east());
found = found || BlockLavahoarder.suckLava(world, pos.west());
found = found || BlockLavahoarder.suckLava(world, pos.up());
found = found || BlockLavahoarder.suckLava(world, pos.down());
if (found) {
world.setBlockState(pos, ModBlocks.BLOCK_LAVAHOARDER.getDefaultState());
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(ModBlocks.BLOCK_LAVAHOARDER, pos));
} else {
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos));
}
}
private static final String stringOf(BlockPos pos) {
StringBuilder buf = new StringBuilder();
buf.append(pos.getX()).append(":");
buf.append(pos.getY()).append(":");
buf.append(pos.getZ());
return buf.toString();
}
private static final BlockPos blockPosOf(String s) {
if (s.contains(":")) {
String[] parts = s.split(":");
if (parts.length > 2) {
Integer x = Integer.valueOf(parts[0]);
Integer y = Integer.valueOf(parts[1]);
Integer z = Integer.valueOf(parts[2]);
return new BlockPos(x, y, z);
} else {
return null;
}
} else {
return null;
}
}
private void findAllAttachedLavaBlocks(Set<String> list, BlockPos pos, World world, Integer counter) {
if (counter < 1) {
return;
} else if (Blocks.LAVA.equals(world.getBlockState(pos).getBlock())) {
String p = stringOf(pos);
if (!list.contains(p)) {
list.add(p);
findAllAttachedLavaBlocks(list, pos.up(), world, counter - 1);
findAllAttachedLavaBlocks(list, pos.down(), world, counter - 1);
findAllAttachedLavaBlocks(list, pos.north(), world, counter - 1);
findAllAttachedLavaBlocks(list, pos.south(), world, counter - 1);
findAllAttachedLavaBlocks(list, pos.east(), world, counter - 1);
findAllAttachedLavaBlocks(list, pos.west(), world, counter - 1);
}
}
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
Set<String> positions = new HashSet<>();
Integer counter = 8; // TODO: make it level up - able
findAllAttachedLavaBlocks(positions, pos.up(), world, counter);
findAllAttachedLavaBlocks(positions, pos.down(), world, counter);
findAllAttachedLavaBlocks(positions, pos.north(), world, counter);
findAllAttachedLavaBlocks(positions, pos.south(), world, counter);
findAllAttachedLavaBlocks(positions, pos.east(), world, counter);
findAllAttachedLavaBlocks(positions, pos.west(), world, counter);
Integer amount = positions.size();
for (String p : positions) {
world.setBlockState(blockPosOf(p), Blocks.AIR.getDefaultState());
}
if (amount > 0) {
world.setBlockState(pos, ModBlocks.BLOCK_LAVAHOARDER.getDefaultState());
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(ModBlocks.BLOCK_LAVAHOARDER, pos));
int count = 0;
Random random = new Random();
for (int i = 0; i < amount; i++) {
if (random.nextFloat() < 0.0125) {
count++;
}
}
BlockPos up = pos.up();
if (count > 0) {
BlockLavahoarder.spawnRandomItems(world, up, count);
world.spawnEntity(new ExperienceOrbEntity(world, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D,
(double) pos.getZ() + 0.5D, count));
}
} else {
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos));
}
}
}

View File

@ -0,0 +1,135 @@
package de.jottyfan.quickiemod.block;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import de.jottyfan.quickiemod.item.ModItems;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.loot.context.LootWorldContext.Builder;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.tick.OrderedTick;
/**
*
* @author jotty
*
*/
public class BlockLavahoarder extends Block {
public BlockLavahoarder(Identifier identifier) {
super(AbstractBlock.Settings.create().hardness(2.5f).luminance(state -> 15)
.registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
}
@Override
public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) {
List<ItemStack> list = new ArrayList<>();
list.add(new ItemStack(ModBlocks.BLOCK_LAVAHOARDER));
return list;
}
public static final void spawnRandomItems(World world, BlockPos pos, Integer count) {
Integer which = new Random().nextInt(10);
if (which < 1) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(),
new ItemStack(Items.DIAMOND, new Random().nextInt(count + 2))));
} else if (which < 2) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(),
new ItemStack(Items.EMERALD, new Random().nextInt(count + 1))));
} else if (which < 3) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(),
new ItemStack(Items.RAW_GOLD, new Random().nextInt(count))));
} else if (which < 4) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(),
new ItemStack(Items.RAW_IRON, new Random().nextInt(count + 1))));
} else if (which < 5) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(),
new ItemStack(Items.RAW_COPPER, new Random().nextInt(count + 2))));
} else if (which < 6) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.OBSIDIAN)));
} else if (which < 7) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.LAPIS_LAZULI)));
} else if (which < 8) {
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(ModItems.ITEM_SULFOR)));
}
}
/**
* sucks the lava that touches the block
*
* @param world the world
* @param pos the pos
* @return true if lava was found
*/
public static final boolean suckLava(World world, BlockPos pos) {
if (world == null) {
return false;
} else if (Blocks.LAVA.equals(world.getBlockState(pos).getBlock())) {
world.setBlockState(pos, Blocks.AIR.getDefaultState());
BlockPos up = pos.up();
Random random = new Random();
if (random.nextFloat() > 0.9f) {
spawnRandomItems(world, up, 2);
}
return true;
}
return false;
}
@Override
protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos,
net.minecraft.util.math.random.Random random) {
suckLava(world, pos.north());
suckLava(world, pos.south());
suckLava(world, pos.east());
suckLava(world, pos.west());
suckLava(world, pos.up());
suckLava(world, pos.down());
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos));
}
@Override
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (!world.isClient) {
Hand hand = player.getActiveHand();
ItemStack handStack = player.getStackInHand(hand);
if (handStack != null && Items.BUCKET.equals(handStack.getItem())) {
Integer amount = handStack.getCount();
ItemStack lavaBucketStack = new ItemStack(Items.LAVA_BUCKET, 1);
ItemStack emptyBucketStack = new ItemStack(Items.BUCKET, amount - 1);
if (emptyBucketStack.getCount() < 1) {
player.setStackInHand(hand, lavaBucketStack);
} else {
player.setStackInHand(hand, emptyBucketStack);
world.spawnEntity(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), lavaBucketStack));
}
spawnRandomItems(world, pos, 2);
world.setBlockState(pos, ModBlocks.BLOCK_EMPTYLAVAHOARDER.getDefaultState());
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(ModBlocks.BLOCK_EMPTYLAVAHOARDER, pos));
}
}
return ActionResult.SUCCESS; // forbid to empty the just filled lava bucket
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos));
}
}

View File

@ -33,6 +33,10 @@ public class ModBlocks {
ModIdentifiers.BLOCK_SULFOR, 1.5f, 1.5f, new ItemStack[] { new ItemStack(ModItems.ITEM_SULFOR, 9) }));
public static final Block BLOCK_MONSTERHOARDER = registerBlock(ModIdentifiers.BLOCK_MONSTERHOARDER,
new BlockMonsterhoarder(ModIdentifiers.BLOCK_MONSTERHOARDER));
public static final Block BLOCK_LAVAHOARDER = registerBlock(ModIdentifiers.BLOCK_LAVAHOARDER,
new BlockLavahoarder(ModIdentifiers.BLOCK_LAVAHOARDER));
public static final Block BLOCK_EMPTYLAVAHOARDER = registerBlock(ModIdentifiers.BLOCK_EMPTYLAVAHOARDER,
new BlockEmptyLavahoarder(ModIdentifiers.BLOCK_EMPTYLAVAHOARDER));
private static final Block registerBlock(Identifier identifier, Block block) {
Registry.register(Registries.ITEM, identifier, new BlockItem(block, new Item.Settings()
@ -49,6 +53,8 @@ public class ModBlocks {
blocks.add(BLOCK_SALPETER);
blocks.add(BLOCK_SULFOR);
blocks.add(BLOCK_MONSTERHOARDER);
blocks.add(BLOCK_EMPTYLAVAHOARDER);
blocks.add(BLOCK_LAVAHOARDER);
return blocks;
}
}

View File

@ -25,4 +25,6 @@ public class ModIdentifiers {
public static final Identifier BLOCK_SALPETER = Identifier.of(Quickiemod.MOD_ID, "blocksalpeter");
public static final Identifier BLOCK_SULFOR = Identifier.of(Quickiemod.MOD_ID, "blocksulphor");
public static final Identifier BLOCK_MONSTERHOARDER = Identifier.of(Quickiemod.MOD_ID, "monsterhoarder");
public static final Identifier BLOCK_LAVAHOARDER = Identifier.of(Quickiemod.MOD_ID, "lavahoarder");;
public static final Identifier BLOCK_EMPTYLAVAHOARDER = Identifier.of(Quickiemod.MOD_ID, "emptylavahoarder");;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,10 @@
{
"parent": "quickiemod:block/emptylavahoarder",
"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/lavahoarder",
"display": {
"thirdperson": {
"rotation": [ 10, -45, 170 ],
"translation": [ 0, 1.5, -2.75 ],
"scale": [ 0.375, 0.375, 0.375 ]
}
}
}

View File

@ -2,6 +2,9 @@
"replace": false,
"values": [
"quickiemod:blocksulphor",
"quickiemod:blocksalpeter"
"quickiemod:blocksalpeter",
"quickiemod:monsterhoarder",
"quickiemod:lavahoarder",
"quickiemod:emptylavahoarder"
]
}

View File

@ -0,0 +1,17 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
"oio",
"ibi",
"oio"
],
"key": {
"o": "minecraft:obsidian",
"i": "quickiemod:quickieingot",
"b": "minecraft:bucket"
},
"result": {
"id": "quickiemod:emptylavahoarder",
"count": 1
}
}