Skip to content

Commit

Permalink
Fix #7, #8, and #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Sep 22, 2024
1 parent a12ba98 commit cc68bcd
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
Expand All @@ -18,11 +19,17 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;


public class UseBlockHandler {
public static InteractionResult onBlockUse(Player player, Level world, InteractionHand hand, BlockPos pos, Direction direction) {
if (!player.mayBuild()) return InteractionResult.PASS;
public static InteractionResult onBlockUse(@Nullable Player player, Level world, InteractionHand hand, BlockPos pos, Direction direction) {
if (player == null)
return InteractionResult.PASS;

if (!player.mayBuild())
return InteractionResult.PASS;

var state = world.getBlockState(pos);
var stack = player.getItemInHand(hand);
var shouldSneak = false;
Expand All @@ -32,43 +39,49 @@ public static InteractionResult onBlockUse(Player player, Level world, Interacti
var isBasicallySneaking = player.isShiftKeyDown() || !shouldSneak;
if (isBasicallySneaking) {
var test = testPos(pos, state, player, world, hand, stack);
if (test != InteractionResult.PASS) return test;
if (test != InteractionResult.PASS)
return test;
}
if (state.hasProperty(Sculkable.SCULK_INFESTED) && !isBasicallySneaking) return InteractionResult.PASS;
if (state.hasProperty(Sculkable.SCULK_INFESTED) && !isBasicallySneaking)
return InteractionResult.PASS;
var hitPos = pos.relative(direction);
var hitState = world.getBlockState(hitPos);
return testPos(hitPos, hitState, player, world, hand, stack);
}

public static InteractionResult testPos(BlockPos pos, BlockState state, Player player, Level world, InteractionHand hand, ItemStack stack) {
if (!state.hasProperty(Sculkable.SCULK_INFESTED)) return InteractionResult.PASS;
if (!state.hasProperty(Sculkable.SCULK_INFESTED))
return InteractionResult.PASS;

boolean isInfested = state.getValue(Sculkable.SCULK_INFESTED);

if (stack.getItem() instanceof ShearsItem && isInfested) {
world.setBlockAndUpdate(pos, state.setValue(Sculkable.SCULK_INFESTED, false));
if (player instanceof ServerPlayer serverPlayer) {
if (player instanceof ServerPlayer serverPlayer)
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(serverPlayer, pos, stack);
}

world.playSound(player, pos, SoundEvents.SHEEP_SHEAR, SoundSource.BLOCKS, 0.5F, 1.0F);
world.playSound(player, pos, SoundEvents.SCULK_BLOCK_BREAK, SoundSource.BLOCKS, 1.5F, 1.0F);

stack.hurtAndBreak(1, player, hand == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND);
if (!player.isCreative()) {
var item = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, Items.SCULK_VEIN.getDefaultInstance());
world.addFreshEntity(item);
}
world.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(state));

return InteractionResult.SUCCESS;
} else if (stack.getItem() == Items.SCULK_VEIN && !isInfested) {
world.setBlockAndUpdate(pos, state.setValue(Sculkable.SCULK_INFESTED, true));
if (player instanceof ServerPlayer serverPlayer) {
if (player instanceof ServerPlayer serverPlayer)
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(serverPlayer, pos, stack);
}

world.playSound(player, pos, SoundEvents.SCULK_BLOCK_PLACE, SoundSource.BLOCKS, 1.0F, 1.0F);
if (!player.isCreative()) {
if (!player.isCreative())
stack.shrink(1);
}

world.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(state));

return InteractionResult.SUCCESS;
}
return InteractionResult.PASS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package dev.sweetberry.wwizardry.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.sweetberry.wwizardry.content.datagen.DatagenInitializer;
import dev.sweetberry.wwizardry.content.villager.VillagerInitializer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.npc.VillagerDataHolder;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.trading.ItemCost;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.level.ItemLike;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Stream;

@Mixin(targets = {"net/minecraft/world/entity/npc/VillagerTrades$EmeraldsForVillagerTypeItem"})
public class Mixin_VillagerTrades_EmeraldsForVillagerTypeItem {
@Shadow
@Final
private int cost;

@Shadow
@Final
private int maxUses;

@Shadow
@Final
private int villagerXp;

@Shadow
@Final
private Map<VillagerType, Item> trades;

@WrapOperation(
method = "<init>",
at = @At(
value = "INVOKE",
target = "Ljava/util/stream/Stream;filter(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;"
)
)
private Stream<VillagerType> wrapFilter(Stream<VillagerType> instance, Predicate<? super VillagerType> predicate, Operation<Stream<VillagerType>> original) {
return original.call(instance, (Predicate<? super VillagerType>)(it) -> it != VillagerInitializer.FUNGAL_FOREST_VILLAGER.get() && predicate.test(it));
}

@Inject(
method = "getOffer",
at = @At("HEAD"),
cancellable = true
)
private void addWwizardryTrades(Entity entity, RandomSource random, CallbackInfoReturnable<MerchantOffer> cir) {
if (entity instanceof VillagerDataHolder villager && villager.getVariant() == VillagerInitializer.FUNGAL_FOREST_VILLAGER.get()) {
if (villager.getVillagerData().getProfession() == VillagerProfession.FISHERMAN) {
assert DatagenInitializer.DENIA_WOOD.BOAT_ITEM != null;
cir.setReturnValue(new MerchantOffer(new ItemCost(DatagenInitializer.DENIA_WOOD.BOAT_ITEM.get(), cost), new ItemStack(Items.EMERALD), maxUses, villagerXp, 0.05F));
}

// Fall back to plains
ItemCost itemCost = new ItemCost(trades.get(VillagerType.PLAINS), cost);
cir.setReturnValue(new MerchantOffer(itemCost, new ItemStack(Items.EMERALD), maxUses, villagerXp, 0.05F));
}
}
}

This file was deleted.

This file was deleted.

3 changes: 2 additions & 1 deletion common/src/main/resources/wwizardry.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"Mixin_RedStoneWireBlock",
"Mixin_ReloadableResourceManager",
"Mixin_SculkVeinBlock_SculkVeinSpreaderConfig",
"Mixin_StructureTemplate"
"Mixin_StructureTemplate",
"Mixin_VillagerTrades_EmeraldsForVillagerTypeItem"
],
"injectors": {
"defaultRequire": 1
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.parallel = true
org.gradle.daemon=false

# Mod Properties
version = 1.1.0
version = 1.1.1
classification =
maven_group = dev.sweetberry
archives_base_name = wwizardry
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[versions]
minecraft = "1.21"
minecraft = "1.21.1"
fabric_loader = "0.15.11"
neoforge = "21.0.37-beta"
neoforge = "21.1.57"

mixin = "0.8.5"
mixin_extras = "0.3.5"

fabric_api = "0.100.3+1.21"
fabric_api = "0.104.0+1.21.1"

terrablender = "1.21-4.0.0.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.sweetberry.wwizardry.neoforge;

import com.mojang.datafixers.util.Either;
import dev.sweetberry.wwizardry.WanderingWizardry;
import dev.sweetberry.wwizardry.client.WanderingWizardryClient;
import dev.sweetberry.wwizardry.client.content.events.ClientEvents;
import dev.sweetberry.wwizardry.client.content.events.ItemTooltipHandler;
import dev.sweetberry.wwizardry.client.content.events.PackReloader;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.TooltipFlag;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.EntityRenderersEvent;
import net.neoforged.neoforge.client.event.RegisterClientReloadListenersEvent;
import net.neoforged.neoforge.client.event.RenderTooltipEvent;
import net.neoforged.neoforge.common.NeoForge;

public class NeoForgeClientEvents {
public static void init(IEventBus modBus) {
NeoForge.EVENT_BUS.register(NeoForgeClientEvents.class);

ClientEvents.registerModelPredicates((item, name, callback) -> {
ItemProperties.registerGeneric(
WanderingWizardry.id(name),
callback
);
});
modBus.addListener(NeoForgeClientEvents::registerEntityRenderers);
modBus.addListener(NeoForgeClientEvents::registerEntityLayers);
modBus.addListener(NeoForgeClientEvents::registerClientReloadListeners);
WanderingWizardryClient.init();
}

@SubscribeEvent
public static void onTooltip(RenderTooltipEvent.GatherComponents event) {
var lines = event.getTooltipElements();

ItemTooltipHandler.addTooltips(
event.getItemStack(),
TooltipFlag.NORMAL,
(i, c) -> lines.addAll(i,
c.stream().map(it -> (FormattedText) it).map(Either::<FormattedText, TooltipComponent>left).toList()
)
);
}

@SubscribeEvent
public static void onClientTick(ClientTickEvent.Post ev) {
WanderingWizardryClient.tickCounter++;
}

public static void registerClientReloadListeners(RegisterClientReloadListenersEvent ev) {
ev.registerReloadListener(new PackReloader());
}

public static void registerEntityRenderers(EntityRenderersEvent.RegisterRenderers event) {
ClientEvents.registerBlockEntityRenderers((type, renderer) -> {
event.registerBlockEntityRenderer(type.get(), renderer);
});

ClientEvents.registerEntityRenderers((type, renderer) -> {
event.registerEntityRenderer(type.get(), renderer);
});
}

public static void registerEntityLayers(EntityRenderersEvent.RegisterLayerDefinitions event) {
ClientEvents.registerModelLayers(event::registerLayerDefinition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.network.chat.FormattedText;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.TooltipFlag;
import net.neoforged.bus.api.SubscribeEvent;
Expand All @@ -30,28 +31,10 @@ public static void onWanderingTrades(WandererTradesEvent ev) {
ev.getRareTrades().addAll(Arrays.stream(VillagerInitializer.WANDERING_TRADER_OFFERS[1]).toList());
}

@SubscribeEvent
public static void onTooltip(RenderTooltipEvent.GatherComponents event) {
var lines = event.getTooltipElements();

ItemTooltipHandler.addTooltips(
event.getItemStack(),
TooltipFlag.NORMAL,
(i, c) -> lines.addAll(i,
c.stream().map(it -> (FormattedText) it).map(Either::<FormattedText, TooltipComponent>left).toList()
)
);
}

@SubscribeEvent
public static void onClientTick(ClientTickEvent.Post ev) {
WanderingWizardryClient.tickCounter++;
}

@SubscribeEvent
public static void onUseOnBlock(UseItemOnBlockEvent event) {
var result = UseBlockHandler.onBlockUse(
event.getEntity(),
event.getPlayer(),
event.getLevel(),
event.getHand(),
event.getPos(),
Expand Down
Loading

0 comments on commit cc68bcd

Please sign in to comment.