Skip to content

Commit

Permalink
make trade adjustments scale logarithmically and improve rendering, m…
Browse files Browse the repository at this point in the history
…ove loot stuff to owo
  • Loading branch information
gliscowo committed Oct 18, 2021
1 parent b244659 commit c28151e
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 778 deletions.
713 changes: 21 additions & 692 deletions LICENSE

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ repositories {
}

maven { url "https://maven.shedaniel.me/" }

maven { url 'https://jitpack.io' }
}

dependencies {
Expand All @@ -34,6 +36,10 @@ dependencies {
modCompileOnly "me.shedaniel:RoughlyEnoughItems-api-fabric:${project.rei_version}"
modRuntime "me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}"

modRuntime "com.ptsmods:devlogin:1.2"

modImplementation "com.github.glisco03:owo-lib:${project.owo_version}"

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ archives_base_name=numismatic-overhaul
fabric_version=0.36.0+1.17
cardinal_components_version=3.0.0
rei_version=6.0.246-alpha
owo_version=0.3.0-fixed-rei
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.glisco.numismaticoverhaul.network.ShopScreenHandlerRequestC2SPacket;
import com.glisco.numismaticoverhaul.villagers.data.VillagerTradesResourceListener;
import com.glisco.numismaticoverhaul.villagers.json.VillagerTradesHandler;
import com.glisco.owo.ops.LootOps;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.loot.v1.FabricLootPoolBuilder;
Expand All @@ -26,10 +27,7 @@
import net.minecraft.item.ItemGroup;
import net.minecraft.loot.LootTables;
import net.minecraft.loot.condition.RandomChanceLootCondition;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.loot.entry.LootPoolEntryType;
import net.minecraft.loot.function.SetCountLootFunction;
import net.minecraft.loot.provider.number.UniformLootNumberProvider;
import net.minecraft.resource.ResourceType;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.util.Identifier;
Expand All @@ -45,7 +43,7 @@ public class NumismaticOverhaul implements ModInitializer {
public static final Item SILVER_COIN = new CoinItem(CurrencyResolver.Currency.SILVER);
public static final Item GOLD_COIN = new CoinItem(CurrencyResolver.Currency.GOLD);

public static final Item MONEY_BAG = new MoneyBagItem();
public static final MoneyBagItem MONEY_BAG = new MoneyBagItem();

public static final Block SHOP_BLOCK = new ShopBlock();
public static BlockEntityType<ShopBlockEntity> SHOP_BLOCK_ENTITY;
Expand Down Expand Up @@ -84,25 +82,21 @@ public void onInitialize() {
VillagerTradesHandler.broadcastErrors(server);
});

LootTableLoadingCallback.EVENT.register((resourceManager, manager, id, supplier, setter) -> {
LootOps.injectItem(GOLD_COIN, .01f, LootTables.STRONGHOLD_LIBRARY_CHEST, LootTables.BASTION_TREASURE_CHEST, LootTables.STRONGHOLD_CORRIDOR_CHEST,
LootTables.PILLAGER_OUTPOST_CHEST, LootTables.BURIED_TREASURE_CHEST, LootTables.SIMPLE_DUNGEON_CHEST, LootTables.ABANDONED_MINESHAFT_CHEST);

LootOps.injectItemWithCount(BRONZE_COIN, .5f, 9, 34, new Identifier("entities/pillager"));
LootOps.injectItem(SILVER_COIN, .2f, new Identifier("entities/pillager"));

LootTableLoadingCallback.EVENT.register((resourceManager, manager, id, supplier, setter) -> {
if (anyMatch(id, LootTables.SIMPLE_DUNGEON_CHEST, LootTables.ABANDONED_MINESHAFT_CHEST)) {
supplier.withPool(FabricLootPoolBuilder.builder().withEntry(MoneyBagLootEntry.builder(500, 2000).build()).conditionally(RandomChanceLootCondition.builder(0.75f))
.withEntry(ItemEntry.builder(GOLD_COIN).conditionally(RandomChanceLootCondition.builder(0.01f)).build())
.build());
} else if (anyMatch(id, LootTables.BASTION_TREASURE_CHEST, LootTables.STRONGHOLD_CORRIDOR_CHEST, LootTables.PILLAGER_OUTPOST_CHEST, LootTables.BURIED_TREASURE_CHEST)) {
supplier.withPool(FabricLootPoolBuilder.builder().withEntry(MoneyBagLootEntry.builder(1500, 4000).build()).conditionally(RandomChanceLootCondition.builder(0.75f))
.withEntry(ItemEntry.builder(GOLD_COIN).conditionally(RandomChanceLootCondition.builder(0.01f)).build())
.build());
} else if (anyMatch(id, LootTables.STRONGHOLD_LIBRARY_CHEST)) {
supplier.withPool(FabricLootPoolBuilder.builder().withEntry(MoneyBagLootEntry.builder(2000, 6000).build()).conditionally(RandomChanceLootCondition.builder(0.85f))
.withEntry(ItemEntry.builder(GOLD_COIN).conditionally(RandomChanceLootCondition.builder(0.01f)).build())
.build());
} else if (id.equals(new Identifier("entities/pillager"))) {
supplier.withPool(FabricLootPoolBuilder.builder()
.withEntry(ItemEntry.builder(BRONZE_COIN).apply(SetCountLootFunction.builder(UniformLootNumberProvider.create(9, 34))).build())
.withEntry(ItemEntry.builder(SILVER_COIN).conditionally(RandomChanceLootCondition.builder(0.4f)).build())
.conditionally(RandomChanceLootCondition.builder(0.5f))
.build());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void renderTooltip(CurrencyStack stack, @Nullable CurrencyStack be
if (!coinsBefore.isEmpty()) tooltip.add(Text.of(" "));

for (int i = 0; i < coins.size(); i++) {
renderStack(matrices, coins.get(i), tooltip, i + coinsBefore.size() + 1, x, y, screen.getZOffset() + 1000, false);
renderStack(matrices, coins.get(i), tooltip, i + (coinsBefore.isEmpty() ? 0 : coinsBefore.size() + 1), x, y, screen.getZOffset() + 1000, false);
}

if (tooltip.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public void silentModify(int value) {
* @param value The value this component should be modified by
*/
public void pushTransaction(int value) {
System.out.println("Pushing " + value);
this.transactions.add(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.glisco.numismaticoverhaul.currency;

import com.glisco.numismaticoverhaul.NumismaticOverhaul;
import com.glisco.numismaticoverhaul.item.CoinItem;
import com.glisco.numismaticoverhaul.item.MoneyBagItem;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CurrencyHelper {
Expand Down Expand Up @@ -40,8 +42,8 @@ public static int getValue(List<ItemStack> stacks) {

if (stack.getItem() instanceof CoinItem) {
return ((CoinItem) stack.getItem()).currency.getRawValue(stack.getCount());
} else if (stack.getItem() instanceof MoneyBagItem) {
return MoneyBagItem.getValue(stack);
} else if (stack.isOf(NumismaticOverhaul.MONEY_BAG)) {
return NumismaticOverhaul.MONEY_BAG.getValue(stack);
} else {
return 0;
}
Expand Down Expand Up @@ -85,4 +87,16 @@ public static List<ItemStack> getAsStacks(CurrencyStack stack, int maxStacks) {
return stacks;
}

public static ItemStack round(CurrencyStack stack) {
var values = CurrencyResolver.splitValues(stack.getRawValue());

for (int i = 0; i < 2; i++) {
if (values[i + 1] == 0) break;
values[i + 1] += Math.round(values[i] / 100f);
values[i] = 0;
}

return getAsStacks(new CurrencyStack(CurrencyResolver.combineValues(values)), 1).get(0);
}

}
12 changes: 11 additions & 1 deletion src/main/java/com/glisco/numismaticoverhaul/item/CoinItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

public class CoinItem extends Item {
public class CoinItem extends Item implements CurrencyItem {

public final CurrencyResolver.Currency currency;

Expand Down Expand Up @@ -49,4 +49,14 @@ public Text getName() {
public Text getName(ItemStack stack) {
return super.getName(stack).copy().setStyle(NAME_STYLE);
}

@Override
public boolean wasAdjusted(ItemStack other) {
return other.getItem() != this;
}

@Override
public int getValue(ItemStack stack) {
return this.currency.getRawValue(stack.getCount());
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/glisco/numismaticoverhaul/item/CurrencyItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.glisco.numismaticoverhaul.item;

import net.minecraft.item.ItemStack;

public interface CurrencyItem {

static void setOriginalValue(ItemStack stack, int value) {
stack.getOrCreateTag().putInt("OriginalValue", value);
}

static int getOriginalValue(ItemStack stack) {
return stack.getOrCreateTag().getInt("OriginalValue");
}

static boolean hasOriginalValue(ItemStack stack) {
return stack.getOrCreateTag().contains("OriginalValue");
}

boolean wasAdjusted(ItemStack other);

int getValue(ItemStack stack);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.util.List;

public class MoneyBagItem extends Item {
public class MoneyBagItem extends Item implements CurrencyItem {

public MoneyBagItem() {
super(new Settings().group(ItemGroup.MISC).maxCount(1));
Expand All @@ -34,7 +34,7 @@ public static ItemStack create(int value) {
return stack;
}

public static int getValue(ItemStack stack) {
public int getValue(ItemStack stack) {
return stack.getOrCreateTag().getInt("Value");
}

Expand Down Expand Up @@ -78,4 +78,9 @@ public void inventoryTick(ItemStack stack, World world, Entity entity, int slot,
player.getInventory().offerOrDrop(toOffer);
}
}

@Override
public boolean wasAdjusted(ItemStack other) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.glisco.numismaticoverhaul.mixin;

import com.glisco.numismaticoverhaul.ModComponents;
import com.glisco.numismaticoverhaul.NumismaticOverhaul;
import com.glisco.numismaticoverhaul.currency.CurrencyComponent;
import com.glisco.numismaticoverhaul.currency.CurrencyHelper;
import com.glisco.numismaticoverhaul.item.CoinItem;
import com.glisco.numismaticoverhaul.item.MoneyBagItem;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.MerchantScreenHandler;
Expand All @@ -34,15 +30,15 @@ public void autofillOverride(int slot, ItemStack stack, CallbackInfo ci) {
CurrencyComponent playerBalance = ModComponents.CURRENCY.get(((PlayerInventory) handler.getSlot(3).inventory).player);

if (stack.getItem() instanceof CoinItem) {
autofillWithCoins(slot, stack, handler, playerBalance);
} else if (stack.getItem() == NumismaticOverhaul.MONEY_BAG) {
numismatic$autofillWithCoins(slot, stack, handler, playerBalance);
}/*else if (stack.getItem() == NumismaticOverhaul.MONEY_BAG) {
autofillWithMoneyBag(slot, stack, handler, playerBalance);
}
}*/

if (slot == 1) playerBalance.commitTransactions();
}

private static void autofillWithCoins(int slot, ItemStack stack, MerchantScreenHandler handler, CurrencyComponent playerBalance) {
private static void numismatic$autofillWithCoins(int slot, ItemStack stack, MerchantScreenHandler handler, CurrencyComponent playerBalance) {
//See how much is required and how much was already autofilled
int requiredCurrency = ((CoinItem) stack.getItem()).currency.getRawValue(stack.getCount());
int presentCurrency = ((CoinItem) stack.getItem()).currency.getRawValue(handler.getSlot(slot).getStack().getCount());
Expand All @@ -60,12 +56,12 @@ private static void autofillWithCoins(int slot, ItemStack stack, MerchantScreenH
handler.slots.get(slot).setStack(stack.copy());
}

private static void autofillWithMoneyBag(int slot, ItemStack stack, MerchantScreenHandler handler, CurrencyComponent playerBalance) {
/*private static void autofillWithMoneyBag(int slot, ItemStack stack, MerchantScreenHandler handler, CurrencyComponent playerBalance) {
PlayerEntity player = ((PlayerInventory) handler.getSlot(3).inventory).player;
//See how much is required and how much in present in the player's inventory
int requiredCurrency = MoneyBagItem.getValue(stack);
int requiredCurrency = NumismaticOverhaul.MONEY_BAG.getValue(stack);
int availableCurrencyInPlayerInventory = CurrencyHelper.getMoneyInInventory(player, false);
//Find out how much we still need to fill
Expand All @@ -82,7 +78,7 @@ private static void autofillWithMoneyBag(int slot, ItemStack stack, MerchantScre
}
handler.slots.get(slot).setStack(stack.copy());
}
}*/

@Inject(method = "playYesSound", at = @At("HEAD"), cancellable = true)
public void checkForEntityOnYes(CallbackInfo ci) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.glisco.numismaticoverhaul.client.gui.CurrencyTooltipRenderer;
import com.glisco.numismaticoverhaul.currency.CurrencyResolver;
import com.glisco.numismaticoverhaul.currency.CurrencyStack;
import com.glisco.numismaticoverhaul.item.MoneyBagItem;
import com.glisco.numismaticoverhaul.item.CurrencyItem;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.MerchantScreen;
import net.minecraft.client.util.math.MatrixStack;
Expand All @@ -26,36 +26,42 @@ private MerchantScreenMixin(Text title) {
super(title);
}

private ItemStack numismatic$firstBuyItem;
private ItemStack numismatic$adjustedFirstBuyItem;
private ItemStack numismatic$originalFirstBuyItem;

@Inject(method = "renderFirstBuyItem", at = @At("HEAD"))
private void captureFirstBuyItem(MatrixStack matrices, ItemStack adjustedFirstBuyItem, ItemStack originalFirstBuyItem, int x, int y, CallbackInfo ci) {
this.numismatic$firstBuyItem = adjustedFirstBuyItem;
this.numismatic$originalFirstBuyItem = originalFirstBuyItem;
this.numismatic$adjustedFirstBuyItem = adjustedFirstBuyItem;
}

@ModifyVariable(method = "renderFirstBuyItem", at= @At("HEAD"), argsOnly = true, ordinal = 1)
@ModifyVariable(method = "renderFirstBuyItem", at = @At("HEAD"), argsOnly = true, ordinal = 1)
private ItemStack dontShowBagDiscount(ItemStack original) {
if (numismatic$firstBuyItem.isOf(NumismaticOverhaul.MONEY_BAG)) {
var copy = this.numismatic$firstBuyItem;
this.numismatic$firstBuyItem = null;
var adjustedItem = numismatic$adjustedFirstBuyItem.getItem();

if (adjustedItem instanceof CurrencyItem adjustable && adjustable.wasAdjusted(numismatic$originalFirstBuyItem)) {
var copy = this.numismatic$adjustedFirstBuyItem;
this.numismatic$adjustedFirstBuyItem = null;
return copy;
} else {
this.numismatic$firstBuyItem = null;
this.numismatic$adjustedFirstBuyItem = null;
return original;
}
}

@Override
protected void renderTooltip(MatrixStack matrices, ItemStack stack, int x, int y) {
if (stack.getItem().equals(NumismaticOverhaul.MONEY_BAG)) {
if (MoneyBagItem.hasBefore(stack)) {
CurrencyTooltipRenderer.renderTooltip(new CurrencyStack(MoneyBagItem.getValue(stack)), new CurrencyStack(MoneyBagItem.getBefore(stack)), matrices, this, new TranslatableText("item.numismatic-overhaul.money_bag").setStyle(Style.EMPTY.withColor(TextColor.fromRgb(CurrencyResolver.Currency.SILVER.getNameColor()))), x, y);
} else {
CurrencyTooltipRenderer.renderTooltip(new CurrencyStack(MoneyBagItem.getValue(stack)), matrices, this, new TranslatableText("item.numismatic-overhaul.money_bag").setStyle(Style.EMPTY.withColor(TextColor.fromRgb(CurrencyResolver.Currency.SILVER.getNameColor()))), x, y);
}
if (!(stack.getItem() instanceof CurrencyItem currencyItem)) {
super.renderTooltip(matrices, stack, x, y);
return;
}

super.renderTooltip(matrices, stack, x, y);
if (CurrencyItem.hasOriginalValue(stack)) {
CurrencyTooltipRenderer.renderTooltip(new CurrencyStack(currencyItem.getValue(stack)), new CurrencyStack(CurrencyItem.getOriginalValue(stack)), matrices, this, new TranslatableText(stack.getTranslationKey()).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(CurrencyResolver.Currency.SILVER.getNameColor()))), x, y);
} else if (currencyItem == NumismaticOverhaul.MONEY_BAG) {
CurrencyTooltipRenderer.renderTooltip(new CurrencyStack(currencyItem.getValue(stack)), matrices, this, new TranslatableText(stack.getTranslationKey()).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(CurrencyResolver.Currency.SILVER.getNameColor()))), x, y);
} else {
super.renderTooltip(matrices, stack, x, y);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandlerMixin {

//Another :concern: mixin to allow creative cheating of items with stack sizes of 64
//Another :concern: mixin to allow creative cheating of items with stack sizes over 64
@SuppressWarnings("SameReturnValue")
@ModifyConstant(method = "onCreativeInventoryAction", constant = @Constant(intValue = 64))
public int increaseStackLimit(int originalValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.glisco.numismaticoverhaul.mixin;

import com.glisco.numismaticoverhaul.NumismaticOverhaul;
import com.glisco.numismaticoverhaul.currency.CurrencyHelper;
import com.glisco.numismaticoverhaul.currency.CurrencyStack;
import com.glisco.numismaticoverhaul.item.CoinItem;
import com.glisco.numismaticoverhaul.item.CurrencyItem;
import com.glisco.numismaticoverhaul.item.MoneyBagItem;
import com.glisco.numismaticoverhaul.villagers.data.NumismaticTradeOfferExtensions;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -53,10 +53,14 @@ private void adjustFirstStack(CallbackInfoReturnable<ItemStack> cir) {
if (!(this.firstBuyItem.getItem() instanceof CoinItem || this.firstBuyItem.getItem() instanceof MoneyBagItem)) return;

int value = CurrencyHelper.getValue(Collections.singletonList(this.firstBuyItem));
int adjustedValue = (int) (value - numismatic$reputation * (Math.abs(value) * .0075));
int adjustedValue = numismatic$reputation < 0 ?
(int) (value + numismatic$reputation * (Math.abs(value) * .325))
:
(int) Math.max(1, value - Math.abs(value) * (numismatic$reputation / (numismatic$reputation + 100f)));

final var stack = CurrencyHelper.getAsStacks(new CurrencyStack(adjustedValue), 1).get(0);
if (stack.isOf(NumismaticOverhaul.MONEY_BAG)) MoneyBagItem.setBefore(stack, value);
final var stack = CurrencyHelper.round(new CurrencyStack(adjustedValue));
if (value != CurrencyHelper.getValue(Collections.singletonList(stack)) && !stack.isOf(this.firstBuyItem.getItem()))
CurrencyItem.setOriginalValue(stack, value);
cir.setReturnValue(stack);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ protected void apply(Map<Identifier, JsonElement> loader, ResourceManager manage
if (!registry.getRight().isEmpty()) {
TradeOffers.WANDERING_TRADER_TRADES.clear();
TradeOffers.WANDERING_TRADER_TRADES.putAll(registry.getRight());

System.out.println(TradeOffers.WANDERING_TRADER_TRADES);
}

}
Expand Down
Loading

0 comments on commit c28151e

Please sign in to comment.