Skip to content

Commit

Permalink
Merge pull request #15 from GameduJS/1.20.4
Browse files Browse the repository at this point in the history
Port master to 1.20.4
  • Loading branch information
GameduJS authored May 8, 2024
2 parents 0ca7bae + c7d9727 commit bb4c10e
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 93 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.1-SNAPSHOT'
id 'fabric-loom' version '1.6-SNAPSHOT'
id 'maven-publish'
}

Expand Down Expand Up @@ -79,3 +79,5 @@ publishing {
// retrieving dependencies.
}
}


10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.19.2
yarn_mappings=1.19.2+build.28
loader_version=0.14.17
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.11
# Mod Properties
mod_version=1.2-1.19.2
mod_version=1.2-1.20.4
maven_group=de.gamedude
archives_base_name=EasyVillagerTrade
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.75.1+1.19.2
fabric_version=0.97.0+1.20.4
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void onInitialize() {
KeyBindingHelper.registerKeyBinding(keyBinding);
}


public void registerCallbacks() {
UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (hand == Hand.OFF_HAND || hitResult == null || !world.isClient())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EnchantmentArgumentType;
import net.minecraft.command.argument.RegistryEntryArgumentType;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.text.Text;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -34,9 +36,12 @@ public void register(CommandDispatcher<FabricClientCommandSource> dispatcher, Co
dispatcher.register(literal(command_base)
.then(literal("select").then(literal("close").executes(this::executeSelectionClosest)).executes(this::executeSelection))
.then(literal("search")
.then(literal("add").then(argument("maxPrice", IntegerArgumentType.integer()).then(argument("enchantment", EnchantmentArgumentType.enchantment()).executes(this::executeAddTradeRequest).then(argument("level", IntegerArgumentType.integer()).executes(this::executeAddTradeRequest)))))
.then(literal("add").then(argument("maxPrice", IntegerArgumentType.integer()).then(argument("enchantment", RegistryEntryArgumentType.registryEntry(registryAccess, RegistryKeys.ENCHANTMENT))
.executes(this::executeAddTradeRequest).then(argument("level", IntegerArgumentType.integer()).executes(this::executeAddTradeRequest)))))

.then(literal("remove").then(argument("enchantment", RegistryEntryArgumentType.registryEntry(registryAccess, RegistryKeys.ENCHANTMENT))
.executes(this::executeRemoveTradeRequest)))

.then(literal("remove").then(argument("enchantment", EnchantmentArgumentType.enchantment()).executes(this::executeRemoveTradeRequest)))
.then(literal("list").executes(this::executeListTradeRequest)))
.then(literal("execute").executes(this::executeVillagerTrade))
.then(literal("stop").executes(ctx -> {
Expand All @@ -51,7 +56,10 @@ public void register(CommandDispatcher<FabricClientCommandSource> dispatcher, Co


public int executeAddTradeRequest(CommandContext<FabricClientCommandSource> context) {
Enchantment enchantment = context.getArgument("enchantment", Enchantment.class);
RegistryEntry.Reference<?> reference = context.getArgument("enchantment", RegistryEntry.Reference.class);
if(!(reference.value() instanceof Enchantment enchantment))
return 0;

int maxPrice = context.getArgument("maxPrice", Integer.class);
int level = getArgumentOrElse(context, "level", Integer.class, enchantment.getMaxLevel());

Expand All @@ -63,7 +71,10 @@ public int executeAddTradeRequest(CommandContext<FabricClientCommandSource> cont
}

public int executeRemoveTradeRequest(CommandContext<FabricClientCommandSource> context) {
Enchantment enchantment = context.getArgument("enchantment", Enchantment.class);
RegistryEntry.Reference<?> reference = context.getArgument("enchantment", RegistryEntry.Reference.class);
if(!(reference.value() instanceof Enchantment enchantment))
return 0;

modBase.getTradeRequestContainer().removeTradeRequestByEnchantment(enchantment);

boolean multipleLevels = enchantment.getMaxLevel() == 1;
Expand Down Expand Up @@ -109,7 +120,7 @@ private <T> T getArgumentOrElse(CommandContext<FabricClientCommandSource> contex
T t;
try {
t = context.getArgument(argument, argumentClass);
} catch ( IllegalArgumentException e ) {
} catch (IllegalArgumentException e) {
return orElse;
}
return t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public SelectionInterface(EasyVillagerTradeBase modBase) {
public void setLecternPos(BlockPos blockPos) { this.lecternPos = blockPos; }

public int selectClosestToPlayer(ClientPlayerEntity player) {
Optional<BlockPos> closestBlockOptional = BlockPos.findClosest(player.getBlockPos(), 3, 0, blockPos -> player.world.getBlockState(blockPos).getBlock() instanceof LecternBlock);
Optional<BlockPos> closestBlockOptional = BlockPos.findClosest(player.getBlockPos(), 3, 0, blockPos -> player.getWorld().getBlockState(blockPos).getBlock() instanceof LecternBlock);
if(closestBlockOptional.isEmpty()) {
modBase.setState(TradingState.INACTIVE);
return 1;
}
this.lecternPos = closestBlockOptional.get();

this.villager = getClosestEntity(player.world, this.lecternPos);
this.villager = getClosestEntity(player.getWorld(), this.lecternPos);
if(this.villager == null) {
modBase.setState(TradingState.INACTIVE);
return 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import de.gamedude.easyvillagertrade.utils.TradeRequest;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.registry.Registry;

public class TradeRequestInputHandler {

private static final Registry<Enchantment> ENCHANTMENT_REGISTRY = Registry.ENCHANTMENT;
private static final Registry<Enchantment> ENCHANTMENT_REGISTRY = Registries.ENCHANTMENT;

public TradeRequest handleCommandInput(Enchantment enchantment, int inputLevel, int maxPrice) {
int level = mapLevel(enchantment, inputLevel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.CloseHandledScreenC2SPacket;
import net.minecraft.network.packet.s2c.play.EntityStatusS2CPacket;
import net.minecraft.network.packet.s2c.play.OpenScreenS2CPacket;
import net.minecraft.network.packet.s2c.play.SetTradeOffersS2CPacket;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -25,9 +26,10 @@
@Mixin(ClientConnection.class)
public abstract class NetworkPacketMixin {

@Unique
private final EasyVillagerTradeBase modBase = EasyVillagerTrade.getModBase();

@Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/Packet;)V", at = @At("HEAD"), cancellable = true)
@Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;)V", at = @At("HEAD"), cancellable = true)
private void channelRead(ChannelHandlerContext context, Packet<?> packet, CallbackInfo ci) {
if(packet instanceof EntityStatusS2CPacket statusPacket) {
World world = MinecraftClient.getInstance().world;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import de.gamedude.easyvillagertrade.screen.widget.TradeRequestListWidget;
import de.gamedude.easyvillagertrade.utils.TradeRequest;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.text.Text;
import net.minecraft.util.math.ColorHelper;
Expand Down Expand Up @@ -49,60 +48,64 @@ protected void init() {
TextFieldWidget priceTextFieldWidget = new TextFieldWidget(textRenderer, x + 30 + enchantmentWidth + levelWidth, px + 15, priceWidth, 20, Text.of("Price"));

TradeRequestListWidget tradeRequestListWidget = new TradeRequestListWidget(x + 10, px + 80, width - x - px - 20, this.height - px - 50);
modBase.getTradeRequestContainer().getTradeRequests().forEach(tradeRequest -> tradeRequestListWidget.addEntry(new TradeRequestListWidget.TradeRequestEntry(tradeRequest)));
modBase.getTradeRequestContainer().getTradeRequests().forEach(tradeRequestListWidget::addEntry);

this.addDrawableChild(enchantmentInputWidget);
this.addDrawableChild(levelTextFieldWidget);
this.addDrawableChild(priceTextFieldWidget);
this.addDrawableChild(new ButtonWidget(x + 9, px + 15 + 20 + 5, 50, 20, Text.of("Add"), button -> {

ButtonWidget addButton = ButtonWidget.builder(Text.of("Add"), button -> {
TradeRequest request = modBase.getTradeRequestInputHandler().handleGUIInput(enchantmentInputWidget.getText(), levelTextFieldWidget.getText(), priceTextFieldWidget.getText());
if (request != null) {
if (!modBase.getTradeRequestContainer().getTradeRequests().contains(request)) {
tradeRequestListWidget.addEntry(new TradeRequestListWidget.TradeRequestEntry(request));
tradeRequestListWidget.addEntry(request);
modBase.getTradeRequestContainer().addTradeRequest(request);
clearTextFieldWidgets(enchantmentInputWidget, levelTextFieldWidget, priceTextFieldWidget);
}
} else {
enchantmentInputWidget.setEditableColor(ColorHelper.Argb.getArgb(255, 255, 0, 0));
}
}));
}).position(x + 9, px + 15 + 20 + 5).size(50, 20).build();

this.addDrawableChild(new ButtonWidget(x + 70, px + 40, 50, 20, Text.of("Remove"), button -> {
ButtonWidget removeButton = ButtonWidget.builder(Text.of("Remove"), button -> {
Enchantment enchantment = modBase.getTradeRequestInputHandler().getEnchantment(enchantmentInputWidget.getText());
if (enchantment == null) {
enchantmentInputWidget.setEditableColor(ColorHelper.Argb.getArgb(255, 255, 0, 0));
return;
}
for (Iterator<TradeRequestListWidget.TradeRequestEntry> it = tradeRequestListWidget.children().iterator(); it.hasNext(); ) {
TradeRequestListWidget.TradeRequestEntry entry = it.next();
if (entry.tradeRequest.enchantment().getTranslationKey().equals(enchantment.getTranslationKey())) {
modBase.getTradeRequestContainer().removeTradeRequest(entry.tradeRequest);
it.remove();
clearTextFieldWidgets(enchantmentInputWidget, levelTextFieldWidget, priceTextFieldWidget);
}
if (entry.tradeRequest.enchantment().getTranslationKey().equals(enchantment.getTranslationKey())) {
modBase.getTradeRequestContainer().removeTradeRequest(entry.tradeRequest);
it.remove();
clearTextFieldWidgets(enchantmentInputWidget, levelTextFieldWidget, priceTextFieldWidget);
}
}
}));
}).position(x + 70, px + 40).size(50, 20).build();

this.addDrawableChild(addButton);
this.addDrawableChild(removeButton);
this.addDrawableChild(tradeRequestListWidget);
}

@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.renderBackground(matrices);
super.render(matrices, mouseX, mouseY, delta);
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
this.renderBackground(context, mouseX, mouseY, delta);
super.render(context, mouseX, mouseY, delta);

int px = (int) (this.width / 50f);
int x = this.width - px - widgetWidth;

this.textRenderer.draw(matrices, Text.of("Enchantment"), x + 10, px + 6, 0xE0E0E0);
this.textRenderer.draw(matrices, Text.of("Level"), x + 20 + enchantmentWidth, px + 6, 0xE0E0E0);
this.textRenderer.draw(matrices, Text.of("Price"), x + 30 + enchantmentWidth + levelWidth, px + 6, 0xE0E0E0);
context.drawText(textRenderer, "Enchantment", x + 10, px + 6, 0xE0E0E0, false);
context.drawText(textRenderer, "Level", x + 20 + enchantmentWidth, px + 6, 0xE0E0E0, false);
context.drawText(textRenderer, "Price", x + 30 + enchantmentWidth + priceWidth, px + 6, 0xE0E0E0, false);
}

@Override
public void renderBackground(MatrixStack matrices) {
public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) {
int px = (int) (this.width / 50f);
int x = this.width - px - widgetWidth;
DrawableHelper.fill(matrices, x, px, this.width - px, this.height - px, ColorHelper.Argb.getArgb(150, 7, 7, 7));
context.fill(x, px, this.width - px, this.height - px, ColorHelper.Argb.getArgb(150, 7, 7, 7));
}

public void clearTextFieldWidgets(TextFieldWidget... textFieldWidgets){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

import joptsimple.internal.Strings;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.navigation.GuiNavigation;
import net.minecraft.client.gui.navigation.GuiNavigationPath;
import net.minecraft.client.gui.navigation.NavigationDirection;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.text.Text;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.registry.Registry;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

import java.util.Arrays;
import java.util.function.Consumer;

public class EnchantmentInputWidget extends TextFieldWidget {

private static final Registry<Enchantment> ENCHANTMENT_REGISTRY = Registry.ENCHANTMENT;
private static final Registry<Enchantment> ENCHANTMENT_REGISTRY = Registries.ENCHANTMENT;
private String suggestion;

public EnchantmentInputWidget(int x, int y, int width, int height) {
Expand All @@ -32,7 +37,7 @@ private Consumer<String> getChangeListener() {
else
this.setEditableColor(0xE0E0E0);

suggestion = getPossibleEnchantmentNameOrElse(text).toLowerCase().replaceFirst("" + text.toLowerCase().replace("+", ""), "");
suggestion = getPossibleEnchantmentNameOrElse(text).toLowerCase().replaceFirst(text.toLowerCase().replace("+", ""), "");
setSuggestion(suggestion);
};
}
Expand Down Expand Up @@ -63,12 +68,12 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
return super.keyPressed(keyCode, scanCode, modifiers);
}

@Nullable
@Override
public boolean changeFocus(boolean lookForwards) {
if(lookForwards) {
public GuiNavigationPath getNavigationPath(GuiNavigation navigation) {
if (navigation.getDirection() == NavigationDirection.DOWN)
setEnchantmentText();
}
return super.changeFocus(lookForwards);
return super.getNavigationPath(navigation);
}

private void setEnchantmentText() {
Expand Down
Loading

0 comments on commit bb4c10e

Please sign in to comment.