Skip to content

Commit

Permalink
Added MoreJS.postUpdateOffers event
Browse files Browse the repository at this point in the history
  • Loading branch information
LLytho committed Oct 31, 2024
1 parent c2728ed commit ee43ac7
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## Unreleased
- /

- Added `MoreJS.postUpdateOffers` event which triggers after a Villager updates their offers

## [0.13.1] - 2024-09-13

Expand Down
4 changes: 4 additions & 0 deletions example_scripts/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ MoreJS.updateOffer((event) => {
const item = Item.of("minecraft:stick").set("minecraft:attribute_modifiers", attributes);
event.offer.output = item;
});

MoreJS.postUpdateOffers(event => {
event.addTrade(VillagerUtils.createSimpleTrade("minecraft:emerald_block", "minecraft:nether_star"));
})
8 changes: 3 additions & 5 deletions src/main/java/com/almostreliable/morejs/core/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import com.almostreliable.morejs.features.structure.StructureAfterPlaceEventJS;
import com.almostreliable.morejs.features.structure.StructureLoadEventJS;
import com.almostreliable.morejs.features.teleport.EntityTeleportsEventJS;
import com.almostreliable.morejs.features.villager.events.StartTradingEventJS;
import com.almostreliable.morejs.features.villager.events.UpdateOfferEventJS;
import com.almostreliable.morejs.features.villager.events.VillagerTradingEventJS;
import com.almostreliable.morejs.features.villager.events.WandererTradingEventJS;
import com.almostreliable.morejs.features.villager.events.*;
import dev.latvian.mods.kubejs.event.EventGroup;
import dev.latvian.mods.kubejs.event.EventHandler;

Expand All @@ -22,7 +19,8 @@ public interface Events {
EventHandler WANDERING_TRADING = GROUP.server("wandererTrades", () -> WandererTradingEventJS.class);
EventHandler PLAYER_START_TRADING = GROUP.server("playerStartTrading", () -> StartTradingEventJS.class);
EventHandler UPDATE_OFFER = GROUP.server("updateOffer",
() -> UpdateOfferEventJS.class).hasResult();
() -> SingleUpdateOfferEventJS.class).hasResult();
EventHandler POST_UPDATE_OFFERS = GROUP.server("postUpdateOffers", () -> PostUpdateOfferEventJS.class);
EventHandler IS_ENCHANTABLE = GROUP.server("isEnchantable",
() -> EnchantmentTableServerEventJS.class);
EventHandler ENCHANTMENT_TABLE_CHANGED = GROUP.server("enchantmentTableChanged",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.almostreliable.morejs.features.villager.events;

import com.almostreliable.morejs.core.Events;
import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.item.trading.MerchantOffers;

import javax.annotation.Nullable;

public class PostUpdateOfferEventJS extends UpdateOfferEventJS {

@SuppressWarnings("ConstantValue")
public static void invoke(AbstractVillager villager, MerchantOffers allOffers) {
if (villager instanceof Villager v) {
var data = v.getVillagerData();
if (data == null) {
return;
}

if (data.getProfession() == VillagerProfession.NONE) {
return;
}
}

Events.POST_UPDATE_OFFERS.post(new PostUpdateOfferEventJS(villager, allOffers));
}

public PostUpdateOfferEventJS(AbstractVillager villager, MerchantOffers allOffers) {
super(villager, allOffers);
}

public void addOffer(@Nullable MerchantOffer offer) {
if (offer != null) {
getAllOffers().add(offer);
}
}

public void addTrade(VillagerTrades.ItemListing trade) {
var offer = trade.getOffer(getEntity(), getRandom());
addOffer(offer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.almostreliable.morejs.features.villager.events;

import com.google.common.base.Preconditions;
import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.item.trading.MerchantOffers;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;

public class SingleUpdateOfferEventJS extends UpdateOfferEventJS {

private final VillagerTrades.ItemListing[] possibleTrades;
private MerchantOffer offer;
@Nullable private List<VillagerTrades.ItemListing> cachedWandererTrades;

public SingleUpdateOfferEventJS(AbstractVillager villager, MerchantOffers allOffers, VillagerTrades.ItemListing[] possibleTrades, MerchantOffer offer) {
super(villager, allOffers);
this.possibleTrades = possibleTrades;
this.offer = offer;
}

public List<VillagerTrades.ItemListing> getUsedTrades() {
return Arrays.asList(possibleTrades);
}

public MerchantOffer getOffer() {
return offer;
}

public void setOffer(MerchantOffer offer) {
Preconditions.checkNotNull(offer, "Offer must not be null");
this.offer = offer;
}

public void setOffer(VillagerTrades.ItemListing trade) {
MerchantOffer newOffer = trade.getOffer(getEntity(), getLevel().getRandom());
if (newOffer != null) {
this.offer = newOffer;
}
}

@Nullable
public MerchantOffer createRandomOffer() {
return createRandomOffer(getUsedTrades());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.almostreliable.morejs.features.villager.events;

import com.almostreliable.morejs.features.villager.VillagerUtils;
import com.google.common.base.Preconditions;
import dev.latvian.mods.kubejs.entity.KubeLivingEntityEvent;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.LivingEntity;
Expand All @@ -14,19 +13,15 @@
import java.util.Arrays;
import java.util.List;

public class UpdateOfferEventJS implements KubeLivingEntityEvent {
public abstract class UpdateOfferEventJS implements KubeLivingEntityEvent {

private final AbstractVillager villager;
private final MerchantOffers allOffers;
private final VillagerTrades.ItemListing[] possibleTrades;
private MerchantOffer offer;
@Nullable private List<VillagerTrades.ItemListing> cachedWandererTrades;

public UpdateOfferEventJS(AbstractVillager villager, MerchantOffers allOffers, VillagerTrades.ItemListing[] possibleTrades, MerchantOffer offer) {
public UpdateOfferEventJS(AbstractVillager villager, MerchantOffers allOffers) {
this.villager = villager;
this.allOffers = allOffers;
this.possibleTrades = possibleTrades;
this.offer = offer;
}

public RandomSource getRandom() {
Expand Down Expand Up @@ -88,31 +83,6 @@ public MerchantOffers getAllOffers() {
return allOffers;
}

public List<VillagerTrades.ItemListing> getUsedTrades() {
return Arrays.asList(possibleTrades);
}

public MerchantOffer getOffer() {
return offer;
}

public void setOffer(MerchantOffer offer) {
Preconditions.checkNotNull(offer, "Offer must not be null");
this.offer = offer;
}

public void setOffer(VillagerTrades.ItemListing trade) {
MerchantOffer newOffer = trade.getOffer(villager, getLevel().getRandom());
if (newOffer != null) {
this.offer = newOffer;
}
}

@Nullable
public MerchantOffer createRandomOffer() {
return createRandomOffer(getUsedTrades());
}

@Nullable
public MerchantOffer createRandomOffer(List<VillagerTrades.ItemListing> possibleTrades) {
if (possibleTrades.isEmpty()) {
Expand All @@ -121,7 +91,7 @@ public MerchantOffer createRandomOffer(List<VillagerTrades.ItemListing> possible

int i = getLevel().getRandom().nextInt(possibleTrades.size());
VillagerTrades.ItemListing randomListing = possibleTrades.get(i);
return randomListing.getOffer(villager, getLevel().getRandom());
return randomListing.getOffer(getEntity(), getLevel().getRandom());
}

public List<VillagerTrades.ItemListing> getVillagerTrades(VillagerProfession profession) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.almostreliable.morejs.mixin.villager;

import com.almostreliable.morejs.core.Events;
import com.almostreliable.morejs.features.villager.events.UpdateOfferEventJS;
import com.almostreliable.morejs.features.villager.events.SingleUpdateOfferEventJS;
import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef;
import net.minecraft.world.entity.npc.AbstractVillager;
Expand All @@ -18,7 +18,7 @@ public class AbstractVillagerMixin {
@Redirect(method = "addOffersFromItemListings", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/trading/MerchantOffers;add(Ljava/lang/Object;)Z"))
private boolean mid$foo(MerchantOffers offers, Object o, MerchantOffers givenMerchantOffers, VillagerTrades.ItemListing[] possibleTrades, int maxNumbers, @Local(ordinal = 1) LocalIntRef i) {
MerchantOffer offer = (MerchantOffer) o;
var e = new UpdateOfferEventJS((AbstractVillager) (Object) this, offers, possibleTrades, offer);
var e = new SingleUpdateOfferEventJS((AbstractVillager) (Object) this, offers, possibleTrades, offer);
if (Events.UPDATE_OFFER.post(e).interruptFalse()) {
i.set(i.get() - 1);
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.almostreliable.morejs.mixin.villager;

import com.almostreliable.morejs.features.villager.events.PostUpdateOfferEventJS;
import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.world.entity.npc.Villager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Villager.class)
public class VillagerMixin {

@Inject(method = "updateTrades", at = @At(value = "RETURN"))
private void morejs$invokePostUpdateOffer(CallbackInfo ci) {
var self = (AbstractVillager) (Object) this;
PostUpdateOfferEventJS.invoke(self, self.getOffers());
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package com.almostreliable.morejs.mixin.villager;

import com.almostreliable.morejs.core.Events;
import com.almostreliable.morejs.features.villager.events.UpdateOfferEventJS;
import com.almostreliable.morejs.features.villager.events.PostUpdateOfferEventJS;
import com.almostreliable.morejs.features.villager.events.SingleUpdateOfferEventJS;
import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraft.world.entity.npc.WanderingTrader;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.item.trading.MerchantOffers;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(WanderingTrader.class)
public abstract class WanderingTraderMixin {

@Redirect(method = "updateTrades", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/trading/MerchantOffers;add(Ljava/lang/Object;)Z"))
private boolean mid$foo(MerchantOffers offers, Object o) {
MerchantOffer offer = (MerchantOffer) o;
var e = new UpdateOfferEventJS((AbstractVillager) (Object) this,
var e = new SingleUpdateOfferEventJS((AbstractVillager) (Object) this,
offers,
VillagerTrades.WANDERING_TRADER_TRADES.get(2),
offer);
Expand All @@ -27,4 +30,10 @@ public abstract class WanderingTraderMixin {

return offers.add(e.getOffer());
}

@Inject(method = "updateTrades", at = @At(value = "RETURN"))
private void morejs$invokePostUpdateOffer(CallbackInfo ci) {
var self = (AbstractVillager) (Object) this;
PostUpdateOfferEventJS.invoke(self, self.getOffers());
}
}
1 change: 1 addition & 0 deletions src/main/resources/morejs.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"villager.MerchantMenuMixin",
"villager.MerchantOfferAccessor",
"villager.MerchantOfferMixin",
"villager.VillagerMixin",
"villager.VillagerTradesMixin$EmeraldForItemsMixin",
"villager.VillagerTradesMixin$EmeraldsForVillagerTypeItemMixin",
"villager.VillagerTradesMixin$EnchantBookForEmeraldsMixin",
Expand Down

0 comments on commit ee43ac7

Please sign in to comment.