Skip to content

Commit

Permalink
Merge pull request #18 from xkball/1.21
Browse files Browse the repository at this point in the history
Add tonk item
  • Loading branch information
xkball authored Sep 5, 2024
2 parents ce7c043 + 1bfa7a0 commit 5454039
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod_name=Power Tool
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPL-3.0
# The mod version. See https://semver.org/
mod_version=1.4.4
mod_version=1.4.5
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.HangingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
Expand Down Expand Up @@ -106,7 +105,7 @@ public InteractionResult interact(Player p, InteractionHand hand) {
}

var held = p.getItemInHand(hand);
if (held.getItem() == Items.LEAD) {
if (held.getItem() == PowerToolItems.TONK.get()) {
var data = held.get(PowerToolItems.KNOT_DATA);
if (data == null) {
// Connection start.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/teacon/powertool/item/PowerToolItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class PowerToolItems {
"cycle", () -> DataComponentType.<Integer>builder().persistent(Codec.INT).build()
);

public static DeferredHolder<Item,TonkItem> TONK;

public static void register(IEventBus bus) {
ITEMS.register(bus);
ITEMS.register("useless_stick", () -> new Item(new Item.Properties()) {
Expand All @@ -102,6 +104,7 @@ public boolean isFoil(ItemStack stack) {
ITEMS.register("transparent_brush",TransparentBrushItem::new);
ITEMS.register("examine_holo_glass",ExamineHoloGlass::new);
ITEMS.register("command_rune", () -> new CommandRune(new Item.Properties()));
TONK = ITEMS.register("tonk", () -> new TonkItem(new Item.Properties()));
CREATIVE_MODE_TABS.register(bus);
DATA_COMPONENTS.register(bus);
ARMOR_MATERIAL.register(bus);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
package org.teacon.powertool.item;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.LogicalSide;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import org.teacon.powertool.PowerTool;
import org.teacon.powertool.entity.FenceKnotEntity;

@EventBusSubscriber(modid = PowerTool.MODID, bus = EventBusSubscriber.Bus.GAME)
public class ItemEventHandler {
import javax.annotation.ParametersAreNonnullByDefault;

@SubscribeEvent
public static void on(PlayerInteractEvent.RightClickBlock event) {
if (event.getSide() == LogicalSide.SERVER) {
var player = event.getEntity();
var held = player.getItemInHand(event.getHand());
if (held.getItem() != Items.LEAD) {
return;
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class TonkItem extends Item {

public TonkItem(Properties properties) {
super(properties);
}

@Override
public InteractionResult useOn(UseOnContext context) {
if (!context.getLevel().isClientSide() && context.getPlayer() != null) {
var player = context.getPlayer();
var held = context.getItemInHand();
var level = context.getLevel();
var pos = context.getClickedPos();
if(!level.getBlockState(pos).is(BlockTags.FENCES)){
return InteractionResult.PASS;
}
var level = event.getLevel();
var pos = event.getPos();
var range = new AABB(pos.getX() - 7, pos.getY() - 7, pos.getZ() - 7, pos.getX() + 7, pos.getY() + 7, pos.getZ() + 7);
for (var mob : level.getEntitiesOfClass(Mob.class, range)) {
if (mob.getLeashHolder() == player) {
return;
return InteractionResult.PASS;
}
}
range = new AABB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
if (!level.getEntitiesOfClass(FenceKnotEntity.class, range).isEmpty()) {
return;
return InteractionResult.PASS;
}
event.setCanceled(true);
var knot = new FenceKnotEntity(level, pos);
level.addFreshEntity(knot);
knot.playPlacementSound();
Expand All @@ -43,5 +48,6 @@ public static void on(PlayerInteractEvent.RightClickBlock event) {
held.shrink(1);
}
}
return InteractionResult.SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "powertool:item/command_rune"
}
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/powertool/models/item/tonk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "powertool:item/tonk"
}
}

0 comments on commit 5454039

Please sign in to comment.