started with itemhoarder

This commit is contained in:
Jörg Henke 2020-07-31 20:35:04 +02:00
parent 842f04baca
commit a18f18ec2b
11 changed files with 271 additions and 34 deletions

View File

@ -17,6 +17,7 @@ public class QuickieFabric implements ModInitializer {
RegistryManager.registerTools();
RegistryManager.registerEvents();
RegistryManager.registerBlocks();
RegistryManager.registerBlockEntities();
Registry.BIOME.forEach(RegistryManager::handleBiome);
}
}

View File

@ -0,0 +1,130 @@
package de.jottyfan.minecraft.quickiefabric.blockentity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.text.Text;
/**
*
* @author jotty
*
*/
public class ItemHoarderBlockEntity extends BlockEntity {
private final String NBT_STACKS = "stacks";
private final List<ItemStack> stacks;
public ItemHoarderBlockEntity() {
super(QuickieFabricBlockEntity.ITEMHOARDER);
stacks = new ArrayList<>();
}
private final Integer getNextFreeEmptyStackPosition() {
Integer emptyStackPosition = null;
Iterator<ItemStack> it = stacks.iterator();
Integer index = 0;
while (emptyStackPosition == null && it.hasNext()) {
ItemStack s = it.next();
if (s.isEmpty()) {
emptyStackPosition = index;
}
index++;
}
return emptyStackPosition;
}
// TODO: see https://fabricmc.net/wiki/tutorial:containers for a real chest
// TODO: find a way to suck in the surrounding items
// @Override
// public void tick() {
// BlockPos pos = getPos();
// World world = getWorld();
// // list all items around here
// AxisAlignedBB aabb = new AxisAlignedBB(pos).grow(4);
// List<ItemEntity> itemEntities = world.getEntitiesWithinAABB(ItemEntity.class, aabb);
// boolean dirty = false;
// for (ItemEntity itemEntity : itemEntities) {
// if (itemEntity.isAlive()) {
// ItemStack stack = itemEntity.getItem();
// if (stack != null) {
// Integer emptyStackPosition = getNextFreeEmptyStackPosition();
// if (emptyStackPosition == null) {
// } else {
// stacks.set(emptyStackPosition, stack);
// itemEntity.remove();
// dirty = true;
// }
// }
// } else {
// itemEntity.getItem().getItem().getRegistryName().getPath());
// }
// }
// if (dirty) {
// resortStacks();
// }
// }
/**
* resort item stacks to be more efficient
*/
private void resortStacks() {
Map<Text, ItemStack> map = new HashMap<>();
Integer clearAmount = stacks.size();
for (ItemStack stack : stacks) {
ItemStack found = map.get(stack.getName());
if (found == null) {
found = stack;
map.put(stack.getName(), found);
} else {
found.setCount(found.getCount() + stack.getCount());
}
}
for (Integer i = 0; i < clearAmount; i++) {
stacks.set(i, ItemStack.EMPTY);
}
Integer index = 0;
for (ItemStack stack : map.values()) {
stacks.set(index++, stack);
}
}
@Override
public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
ListTag listTag = new ListTag();
for (ItemStack stack : stacks) {
listTag.add(stack.toTag(tag));
}
tag.put(NBT_STACKS, listTag);
return tag;
}
@Override
public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag);
ListTag listTag = (ListTag) tag.get(NBT_STACKS);
ListIterator<Tag> i = listTag.listIterator();
stacks.clear();
while (i.hasNext()) {
CompoundTag foundTag = (CompoundTag) i.next();
ItemStack stack = ItemStack.fromTag(foundTag);
stacks.add(stack);
}
}
public List<ItemStack> getStacks() {
return stacks;
}
}

View File

@ -0,0 +1,12 @@
package de.jottyfan.minecraft.quickiefabric.blockentity;
import net.minecraft.block.entity.BlockEntityType;
/**
*
* @author jotty
*
*/
public class QuickieFabricBlockEntity {
public static BlockEntityType<ItemHoarderBlockEntity> ITEMHOARDER;
}

View File

@ -0,0 +1,26 @@
package de.jottyfan.minecraft.quickiefabric.blocks;
import de.jottyfan.minecraft.quickiefabric.blockentity.ItemHoarderBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.Material;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.world.BlockView;
/**
*
* @author jotty
*
*/
public class BlockItemhoarder extends Block implements BlockEntityProvider {
public BlockItemhoarder() {
super(FabricBlockSettings.of(Material.WOOD).hardness(2.5f));
}
@Override
public BlockEntity createBlockEntity(BlockView arg0) {
return new ItemHoarderBlockEntity();
}
}

View File

@ -13,4 +13,5 @@ public class QuickieBlocks {
public static final BlockOreSulphor ORE_SULPHOR = new BlockOreSulphor();
public static final BlockSandSalpeter SAND_SALPETER = new BlockSandSalpeter();
public static final BlockLavahoarder LAVAHOARDER = new BlockLavahoarder();
public static final BlockItemhoarder ITEMHOARDER = new BlockItemhoarder();
}

View File

@ -6,6 +6,8 @@ import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.jottyfan.minecraft.quickiefabric.blockentity.ItemHoarderBlockEntity;
import de.jottyfan.minecraft.quickiefabric.blockentity.QuickieFabricBlockEntity;
import de.jottyfan.minecraft.quickiefabric.blocks.QuickieBlocks;
import de.jottyfan.minecraft.quickiefabric.event.BreakBlockCallback;
import de.jottyfan.minecraft.quickiefabric.event.EventBlockBreak;
@ -15,6 +17,7 @@ import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
@ -41,33 +44,40 @@ public class RegistryManager {
private static final String QUICKIEFABRIC = "quickiefabric";
public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(QuickieItems.SPEEDPOWDER)).appendItems(stacks -> {
stacks.add(new ItemStack(QuickieItems.SALPETER));
stacks.add(new ItemStack(QuickieItems.SULPHOR));
stacks.add(new ItemStack(QuickieItems.SPEEDPOWDER));
stacks.add(new ItemStack(QuickieItems.LEVELUP));
stacks.add(new ItemStack(QuickieItems.PENCIL));
stacks.add(new ItemStack(QuickieTools.SPEEDPOWDERAXE));
stacks.add(new ItemStack(QuickieTools.SPEEDPOWDERPICKAXE));
stacks.add(new ItemStack(QuickieTools.SPEEDPOWDERSHOVEL));
stacks.add(new ItemStack(QuickieBlocks.DIRT_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.ORE_NETHER_SULPHOR));
stacks.add(new ItemStack(QuickieBlocks.ORE_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.ORE_SAND_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.ORE_SULPHOR));
stacks.add(new ItemStack(QuickieBlocks.SAND_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.LAVAHOARDER));
}).build();
public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(QuickieItems.SPEEDPOWDER))
.appendItems(stacks -> {
stacks.add(new ItemStack(QuickieItems.SALPETER));
stacks.add(new ItemStack(QuickieItems.SULPHOR));
stacks.add(new ItemStack(QuickieItems.SPEEDPOWDER));
stacks.add(new ItemStack(QuickieItems.LEVELUP));
stacks.add(new ItemStack(QuickieItems.PENCIL));
stacks.add(new ItemStack(QuickieTools.SPEEDPOWDERAXE));
stacks.add(new ItemStack(QuickieTools.SPEEDPOWDERPICKAXE));
stacks.add(new ItemStack(QuickieTools.SPEEDPOWDERSHOVEL));
stacks.add(new ItemStack(QuickieBlocks.DIRT_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.ORE_NETHER_SULPHOR));
stacks.add(new ItemStack(QuickieBlocks.ORE_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.ORE_SAND_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.ORE_SULPHOR));
stacks.add(new ItemStack(QuickieBlocks.SAND_SALPETER));
stacks.add(new ItemStack(QuickieBlocks.LAVAHOARDER));
stacks.add(new ItemStack(QuickieBlocks.ITEMHOARDER));
}).build();
private static final void registerBlock(Block block, String name) {
Registry.register(Registry.BLOCK, new Identifier(QUICKIEFABRIC, name), block);
Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, name), new BlockItem(block, new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP)));
}
private static final void registerItem(Item item, String name) {
Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, name), item);
}
public static final void registerBlockEntities() {
QuickieFabricBlockEntity.ITEMHOARDER = Registry.register(Registry.BLOCK_ENTITY_TYPE, QUICKIEFABRIC + ":itemhoarderblockentity",
BlockEntityType.Builder.create(ItemHoarderBlockEntity::new, QuickieBlocks.ITEMHOARDER).build(null));
}
public static final void registerBlocks() {
LOGGER.debug("registering quickiefabric blocks");
registerBlock(QuickieBlocks.DIRT_SALPETER, "dirtsalpeter");
@ -77,6 +87,7 @@ public class RegistryManager {
registerBlock(QuickieBlocks.ORE_SULPHOR, "oresulphor");
registerBlock(QuickieBlocks.SAND_SALPETER, "sandsalpeter");
registerBlock(QuickieBlocks.LAVAHOARDER, "lavahoarder");
registerBlock(QuickieBlocks.ITEMHOARDER, "itemhoarder");
}
public static final void registerItems() {
@ -106,14 +117,22 @@ public class RegistryManager {
/**
* generate ores
*
* @param biome the biome to generate the veins in
* @param target the block to be replaced
* @param block the block that is the replacement
* @param veinsize the size of the vein
* @param count the number of veins in a chunk
* @param bottomOffset the lower limit
* @param topOffset the upper limit
* @param maximum the maximum number of blocks in a vein
* @param biome
* the biome to generate the veins in
* @param target
* the block to be replaced
* @param block
* the block that is the replacement
* @param veinsize
* the size of the vein
* @param count
* the number of veins in a chunk
* @param bottomOffset
* the lower limit
* @param topOffset
* the upper limit
* @param maximum
* the maximum number of blocks in a vein
*/
public static void generateOreForTarget(Biome biome, Target target, Block block, int veinsize, int count, int bottomOffset, int topOffset, int maximum) {
OreFeatureConfig ofc = new OreFeatureConfig(target, block.getDefaultState(), veinsize);
@ -124,18 +143,23 @@ public class RegistryManager {
/**
* generate ore instead of block
*
* @param biome the biome
* @param placeOn the list of blockStates underneath
* @param placeIn the list of blockStates to be replaced
* @param placeUnder the list of blockStates above
* @param block the block to set
* @param chance the chance for the replacement
* @param biome
* the biome
* @param placeOn
* the list of blockStates underneath
* @param placeIn
* the list of blockStates to be replaced
* @param placeUnder
* the list of blockStates above
* @param block
* the block to set
* @param chance
* the chance for the replacement
*/
public static void generateOreInBlocks(Biome biome, List<BlockState> placeOn, List<BlockState> placeIn, List<BlockState> placeUnder, Block block, float chance) {
SimpleBlockFeatureConfig sbfc = new SimpleBlockFeatureConfig(block.getDefaultState(), placeOn, placeIn, placeUnder);
biome.addFeature(GenerationStep.Feature.LOCAL_MODIFICATIONS, Feature.SIMPLE_BLOCK.configure(sbfc).withChance(chance).feature);
}
/**
* add the quickiefabric ores to the biome

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "quickiefabric:block/itemhoarder"
}
}
}

View File

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

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,20 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
"ooo",
"oso",
"ooo"
],
"key": {
"o": {
"item": "quickiefabric:speedpowder"
},
"s": {
"item": "minecraft:chest"
}
},
"result": {
"item": "quickiefabric:itemhoarder",
"count": 1
}
}