Skip to content

Commit

Permalink
Work towards a working Block Tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Griefed committed Nov 12, 2023
1 parent 52a42df commit 944bbde
Show file tree
Hide file tree
Showing 24 changed files with 490 additions and 855 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ public class CommonSubProjectGenerator extends CodeGenerator implements CodeGene
private final String commonGeneratedModBlocksClassTemplate = """
package GROUP.block;
import GROUP.Constants;
import GROUP.platform.Services;
import GROUP.registry.RegistrationProvider;
import GROUP.registry.RegistryObject;
import net.minecraft.core.Registry;
import net.minecraft.world.item.*;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SlabBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import static GROUP.CommonClass.BLOCKS;
import static GROUP.CommonClass.ITEMS;
@SuppressWarnings("unused")
public class GeneratedModBlocks {
Expand All @@ -55,12 +57,10 @@ public static void loadClass() {}
private final String commonGeneratedItemsClassTemplate = """
package GROUP.item;
import GROUP.Constants;
import GROUP.platform.Services;
import GROUP.registry.RegistrationProvider;
import GROUP.registry.RegistryObject;
import net.minecraft.core.Registry;
import net.minecraft.world.item.Item;
import static GROUP.CommonClass.ITEMS;
@SuppressWarnings("unused")
public class GeneratedModItems {
Expand Down
9 changes: 9 additions & 0 deletions common/src/main/java/de/griefed/addemall/CommonClass.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package de.griefed.addemall;

import de.griefed.addemall.block.GeneratedModBlocks;
import de.griefed.addemall.event.KeyInputHandler;
import de.griefed.addemall.item.GeneratedModItems;
import de.griefed.addemall.item.ModItems;
import de.griefed.addemall.platform.Services;
import de.griefed.addemall.registry.RegistrationProvider;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.block.Block;

import java.util.List;

Expand All @@ -17,6 +22,9 @@
// however it will be compatible with all supported mod loaders.
public class CommonClass {

public static final RegistrationProvider<Block> BLOCKS = RegistrationProvider.get(Registry.BLOCK_REGISTRY, Constants.MOD_ID);
public static final RegistrationProvider<Item> ITEMS = RegistrationProvider.get(Registry.ITEM_REGISTRY, Constants.MOD_ID);

// The loader specific projects are able to import and use any code from the common project. This allows you to
// write the majority of your code here and load it from your loader specific projects. This example has some
// code that gets invoked by the entry point of the loader specific projects.
Expand All @@ -39,6 +47,7 @@ public static void init() {
/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/

ModItems.loadClass();
KeyInputHandler.loadClass();
}

public static void onItemTooltip(ItemStack stack, TooltipFlag context, List<Component> tooltip) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package de.griefed.addemall.block;

import de.griefed.addemall.Constants;
import de.griefed.addemall.platform.Services;
import de.griefed.addemall.registry.RegistrationProvider;
import de.griefed.addemall.registry.RegistryObject;
import net.minecraft.core.Registry;
import net.minecraft.world.item.*;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SlabBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.StairBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;

import static de.griefed.addemall.CommonClass.BLOCKS;
import static de.griefed.addemall.CommonClass.ITEMS;

@SuppressWarnings("unused")
public class GeneratedModBlocks {
public static final RegistrationProvider<Block> BLOCKS = RegistrationProvider.get(Registry.BLOCK_REGISTRY, Constants.MOD_ID);
public static final RegistrationProvider<Item> ITEMS = RegistrationProvider.get(Registry.ITEM_REGISTRY, Constants.MOD_ID);

/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/
public static final RegistryObject<Block> GREEN_ZEN = BLOCKS.register("generated/dirt/green_zen_block", () -> new Block(BlockBehaviour.Properties.of(Material.DIRT).sound(SoundType.GRASS).strength(2f, 2f).lightLevel(state -> 4).explosionResistance(0f)));
Expand Down Expand Up @@ -362,5 +360,6 @@ private static Item.Properties itemBuilder() {
}

// Called in the mod initializer / constructor in order to make sure that items are registered
public static void loadClass() {}
public static void loadClass() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.griefed.addemall.event;

import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import org.lwjgl.glfw.GLFW;

public class KeyInputHandler {

public static final String KEY_CATEGORY_ADDEMALL = "key.category.addemall.tool";
public static final String KEY_CHANGE_TOOL_BEHAVIOUR = "key.addemall.tool.change_behaviour";
public static final String KEY_SWITCH_TOOL_SHOVEL_HOE = "key.addemall.tool.shov_hoe";

public static KeyMapping toolBehaviourKey;
public static KeyMapping toolShovelHoeKey;

public static boolean TOOL_BEHAVIOUR = false;
public static boolean TOOL_SHOVEL_HOE = false;

public static void loadClass() {}
}
18 changes: 0 additions & 18 deletions common/src/main/java/de/griefed/addemall/item/BlockTool.java

This file was deleted.

235 changes: 235 additions & 0 deletions common/src/main/java/de/griefed/addemall/item/BlockToolItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package de.griefed.addemall.item;

import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.util.Pair;
import de.griefed.addemall.CommonClass;
import de.griefed.addemall.event.KeyInputHandler;
import de.griefed.addemall.platform.Services;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;

import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;

import static net.minecraft.world.item.HoeItem.changeIntoState;
import static net.minecraft.world.item.HoeItem.changeIntoStateAndDropItem;

public class BlockToolItem extends DiggerItem {

public BlockToolItem() {
super(12f, 12f, Tiers.NETHERITE, BlockTags.MINEABLE_WITH_PICKAXE, new Item.Properties().stacksTo(1).defaultDurability(0).durability(0).fireResistant().tab(Services.PLATFORM.getCreativeTab()));
}

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
//TODO axe, shovel, hoe usages
//TODO if air, open block selector
if (hand != InteractionHand.MAIN_HAND) {
return super.use(level, player, hand);
}
if (level.isClientSide()) {
return super.use(level, player, hand);
}
CommonClass.BLOCKS.getEntries().forEach(block -> player.sendSystemMessage(block.get().getName()));
return super.use(level, player, hand);
}

@Override
public InteractionResult useOn(UseOnContext context) {
Level level = context.getLevel();
BlockPos blockPos = context.getClickedPos();
Player player = context.getPlayer();
BlockState blockState = level.getBlockState(blockPos);
Block block = blockState.getBlock();
ItemStack itemInHand = context.getItemInHand();
if (player != null && player.isSecondaryUseActive() && KeyInputHandler.TOOL_BEHAVIOUR) {
// Axe
Optional<BlockState> strippedState = getStripped(blockState);
Optional<BlockState> previousState = WeatheringCopper.getPrevious(blockState);
Optional<BlockState> waxedState = Optional.ofNullable(HoneycombItem.WAX_OFF_BY_BLOCK.get().get(block)).map($$1x -> $$1x.withPropertiesOf(blockState));
Optional<BlockState> optionalBlockState = Optional.empty();
if (strippedState.isPresent()) {
level.playSound(player, blockPos, SoundEvents.AXE_STRIP, SoundSource.BLOCKS, 1.0F, 1.0F);
optionalBlockState = strippedState;
} else if (previousState.isPresent()) {
level.playSound(player, blockPos, SoundEvents.AXE_SCRAPE, SoundSource.BLOCKS, 1.0F, 1.0F);
level.levelEvent(player, 3005, blockPos, 0);
optionalBlockState = previousState;
} else if (waxedState.isPresent()) {
level.playSound(player, blockPos, SoundEvents.AXE_WAX_OFF, SoundSource.BLOCKS, 1.0F, 1.0F);
level.levelEvent(player, 3004, blockPos, 0);
optionalBlockState = waxedState;
}
if (optionalBlockState.isPresent()) {
if (player instanceof ServerPlayer) {
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger((ServerPlayer) player, blockPos, itemInHand);
}
level.setBlock(blockPos, optionalBlockState.get(), 11);
level.gameEvent(GameEvent.BLOCK_CHANGE, blockPos, GameEvent.Context.of(player, optionalBlockState.get()));

return InteractionResult.sidedSuccess(level.isClientSide);

} else if (block instanceof GrowingPlantHeadBlock headBlock && !headBlock.isMaxAge(blockState)) {
// shears
if (player instanceof ServerPlayer) {
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger((ServerPlayer) player, blockPos, itemInHand);
}
level.playSound(player, blockPos, SoundEvents.GROWING_PLANT_CROP, SoundSource.BLOCKS, 1.0F, 1.0F);
level.setBlockAndUpdate(blockPos, headBlock.getMaxAgeState(blockState));

return InteractionResult.sidedSuccess(level.isClientSide);
} else if (KeyInputHandler.TOOL_SHOVEL_HOE) {
// shovel
if (context.getClickedFace() == Direction.DOWN) {
return InteractionResult.PASS;
} else {
BlockState flattenState = FLATTENABLES.get(block);
BlockState updatedState = null;
if (flattenState != null && level.getBlockState(blockPos.above()).isAir()) {
level.playSound(player, blockPos, SoundEvents.SHOVEL_FLATTEN, SoundSource.BLOCKS, 1.0F, 1.0F);
updatedState = flattenState;
} else if (block instanceof CampfireBlock && blockState.getValue(CampfireBlock.LIT)) {
if (!level.isClientSide()) {
level.levelEvent(null, 1009, blockPos, 0);
}

CampfireBlock.dowse(player, level, blockPos, blockState);
updatedState = blockState.setValue(CampfireBlock.LIT, false);
}

if (updatedState != null) {
if (!level.isClientSide) {
level.setBlock(blockPos, updatedState, 11);
level.gameEvent(GameEvent.BLOCK_CHANGE, blockPos, GameEvent.Context.of(player, updatedState));
}

return InteractionResult.sidedSuccess(level.isClientSide);
}
}
} else {
// hoe
Pair<Predicate<UseOnContext>, Consumer<UseOnContext>> consumerPair = TILLABLES.get(block);
if (consumerPair == null) {
return InteractionResult.PASS;
} else {
Predicate<UseOnContext> predicate = consumerPair.getFirst();
Consumer<UseOnContext> consumer = consumerPair.getSecond();
if (predicate.test(context)) {
level.playSound(player, blockPos, SoundEvents.HOE_TILL, SoundSource.BLOCKS, 1.0F, 1.0F);
if (!level.isClientSide) {
consumer.accept(context);
}

return InteractionResult.sidedSuccess(level.isClientSide);
}
}
}
return InteractionResult.PASS;
} else {
// TODO place selected block
// TODO decrement selected block material in tool inv
((BlockItem) CommonClass.BLOCKS.getEntries().stream().toList().get(0).get().asItem()).place(new BlockPlaceContext(context));
}
return super.useOn(context);
}


@Override
public boolean canBeDepleted() {
return false;
}

@Override
public boolean mineBlock(ItemStack stack, Level level, BlockState state, BlockPos pos, LivingEntity entity) {
if (!level.isClientSide()) {
// TODO increment material in tool inv
entity.sendSystemMessage(state.getBlock().getName());
}
//prevent durability decrease
return true;
}

@Override
public boolean isEnchantable(ItemStack stack) {
return false;
}

@Override
public float getDestroySpeed(ItemStack stack, BlockState state) {
return this.speed;
}

@Override
public boolean isCorrectToolForDrops(BlockState state) {
return true;
}

@Override
public boolean canBeHurtBy(DamageSource source) {
// indestructible
return false;
}

private Optional<BlockState> getStripped(BlockState blockState) {
return Optional.ofNullable(STRIPPABLES.get(blockState.getBlock()))
.map(block -> block.defaultBlockState().setValue(RotatedPillarBlock.AXIS, blockState.getValue(RotatedPillarBlock.AXIS)));
}

public static final Map<Block, Pair<Predicate<UseOnContext>, Consumer<UseOnContext>>> TILLABLES = new ImmutableMap.Builder<Block, Pair<Predicate<UseOnContext>, Consumer<UseOnContext>>>()
.put(Blocks.GRASS_BLOCK, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.FARMLAND.defaultBlockState())))
.put(Blocks.DIRT_PATH, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.FARMLAND.defaultBlockState())))
.put(Blocks.DIRT, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.FARMLAND.defaultBlockState())))
.put(Blocks.COARSE_DIRT, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.DIRT.defaultBlockState())))
.put(Blocks.ROOTED_DIRT, Pair.of($$0 -> true, changeIntoStateAndDropItem(Blocks.DIRT.defaultBlockState(), Items.HANGING_ROOTS)))
.build();

public static final Map<Block, BlockState> FLATTENABLES = new ImmutableMap.Builder<Block, BlockState>()
.put(Blocks.GRASS_BLOCK, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.DIRT, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.PODZOL, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.COARSE_DIRT, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.MYCELIUM, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.ROOTED_DIRT, Blocks.DIRT_PATH.defaultBlockState())
.build();

public static final Map<Block, Block> STRIPPABLES = new ImmutableMap.Builder<Block, Block>()
.put(Blocks.OAK_WOOD, Blocks.STRIPPED_OAK_WOOD)
.put(Blocks.OAK_LOG, Blocks.STRIPPED_OAK_LOG)
.put(Blocks.DARK_OAK_WOOD, Blocks.STRIPPED_DARK_OAK_WOOD)
.put(Blocks.DARK_OAK_LOG, Blocks.STRIPPED_DARK_OAK_LOG)
.put(Blocks.ACACIA_WOOD, Blocks.STRIPPED_ACACIA_WOOD)
.put(Blocks.ACACIA_LOG, Blocks.STRIPPED_ACACIA_LOG)
.put(Blocks.BIRCH_WOOD, Blocks.STRIPPED_BIRCH_WOOD)
.put(Blocks.BIRCH_LOG, Blocks.STRIPPED_BIRCH_LOG)
.put(Blocks.JUNGLE_WOOD, Blocks.STRIPPED_JUNGLE_WOOD)
.put(Blocks.JUNGLE_LOG, Blocks.STRIPPED_JUNGLE_LOG)
.put(Blocks.SPRUCE_WOOD, Blocks.STRIPPED_SPRUCE_WOOD)
.put(Blocks.SPRUCE_LOG, Blocks.STRIPPED_SPRUCE_LOG)
.put(Blocks.WARPED_STEM, Blocks.STRIPPED_WARPED_STEM)
.put(Blocks.WARPED_HYPHAE, Blocks.STRIPPED_WARPED_HYPHAE)
.put(Blocks.CRIMSON_STEM, Blocks.STRIPPED_CRIMSON_STEM)
.put(Blocks.CRIMSON_HYPHAE, Blocks.STRIPPED_CRIMSON_HYPHAE)
.put(Blocks.MANGROVE_WOOD, Blocks.STRIPPED_MANGROVE_WOOD)
.put(Blocks.MANGROVE_LOG, Blocks.STRIPPED_MANGROVE_LOG)
.build();
}
Loading

0 comments on commit 944bbde

Please sign in to comment.