Skip to content

Commit

Permalink
Initial port to 1.20.4
Browse files Browse the repository at this point in the history
Includes a small refactor to CurrencyHelper.
CurrencyHelper#getFromNbt is removed as we now use Endec for serialization.

A lot of ShopOffer methods related to NBT were removed as they are no longer necessary

Signed-off-by: Noaaan <noaaan@hotmail.com>
  • Loading branch information
Noaaan committed Mar 9, 2024
1 parent 1024bb6 commit b5372f8
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 129 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//file:noinspection GradlePackageVersionRange
plugins {
id 'fabric-loom' version '1.4-SNAPSHOT'
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'maven-publish'
}

Expand Down
20 changes: 10 additions & 10 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_base_version=1.20.2
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.2
loader_version=0.15.3
minecraft_base_version=1.20.3
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.7

# Mod Properties
mod_version=0.2.14
Expand All @@ -15,19 +15,19 @@ org.gradle.jvmargs=-Xmx1G

# Dependencies
# check this on https://fabricmc.net/develop
fabric_version=0.89.3+1.20.2
fabric_version=0.96.4+1.20.4

# https://github.com/OnyxStudios/Cardinal-Components-API/releases
cca_version=5.3.0
cca_version=5.4.0

# https://maven.shedaniel.me/me/shedaniel/RoughlyEnoughItems-fabric/
rei_version=13.0.666
rei_version=14.0.688

# https://maven.wispforest.io/io/wispforest/owo-lib/
owo_version=0.11.3+1.20.2
# https://maven.wispforest.io/#/releases/io/wispforest/owo-lib/
owo_version=0.12.5+1.20.3

# https://www.curseforge.com/minecraft/mc-mods/modmenu/files
modmenu_version=8.0.0
modmenu_version=9.0.0

# https://storage.googleapis.com/devan-maven/
stacc_version=1.8.0
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.glisco.numismaticoverhaul;

import com.glisco.numismaticoverhaul.currency.Currency;
import com.glisco.numismaticoverhaul.currency.CurrencyConverter;
import com.glisco.numismaticoverhaul.currency.*;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.LongArgumentType;
Expand Down Expand Up @@ -50,7 +49,7 @@ private static int serverWorth(CommandContext<ServerCommandSource> context) {
}

for (var offlineId : offlineUUIDs) {
serverWorth += OfflineDataLookup.get(offlineId).getCompound("cardinal_components").getCompound("numismatic-overhaul:currency").getLong("Value");
serverWorth += OfflineDataLookup.get(offlineId).getCompound("cardinal_components").getCompound("numismatic-overhaul:currency").get(CurrencyHelper.VALUE);
}

long finalServerWorth = serverWorth;
Expand All @@ -67,7 +66,7 @@ private static int set(CommandContext<ServerCommandSource> context) throws Comma
for (var player : players) {
//noinspection deprecation
ModComponents.CURRENCY.get(player).setValue(value);
context.getSource().sendFeedback(() -> TextOps.withColor("numismatic §> balance of " + player.getEntityName() + " set to: " + value,
context.getSource().sendFeedback(() -> TextOps.withColor("numismatic §> balance of " + player.getName().getString() + " set to: " + value,
Currency.GOLD.getNameColor(), TextOps.color(Formatting.GRAY)), false);
}

Expand All @@ -82,7 +81,7 @@ private static int get(CommandContext<ServerCommandSource> context) throws Comma
final long balance = ModComponents.CURRENCY.get(player).getValue();
totalBalance += balance;

context.getSource().sendFeedback(() -> TextOps.withColor("numismatic §> balance of " + player.getEntityName() + ": " + balance,
context.getSource().sendFeedback(() -> TextOps.withColor("numismatic §> balance of " + player.getName().getString() + ": " + balance,
Currency.GOLD.getNameColor(), TextOps.color(Formatting.GRAY)), false);
}

Expand All @@ -102,7 +101,7 @@ private static Command<ServerCommandSource> modify(long multiplier) {
currencyComponent.silentModify(amount * multiplier);
lastValue = currencyComponent.getValue();

context.getSource().sendFeedback(() -> TextOps.withColor("numismatic §> balance of " + player.getEntityName() + " set to: " + currencyComponent.getValue(),
context.getSource().sendFeedback(() -> TextOps.withColor("numismatic §> balance of " + player.getName().getString() + " set to: " + currencyComponent.getValue(),
Currency.GOLD.getNameColor(), TextOps.color(Formatting.GRAY)), false);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.glisco.numismaticoverhaul.block;

import com.glisco.numismaticoverhaul.NumismaticOverhaul;
import com.mojang.serialization.MapCodec;
import io.wispforest.owo.ops.WorldOps;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
Expand All @@ -11,7 +12,6 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.context.LootContextParameterSet;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.screen.NamedScreenHandlerFactory;
Expand All @@ -36,6 +36,7 @@
import java.util.stream.Stream;

public class PiggyBankBlock extends HorizontalFacingBlock implements BlockEntityProvider {
public static final MapCodec<PiggyBankBlock> CODEC = createCodec(ignored -> new PiggyBankBlock());

private static final VoxelShape NORTH_SHAPE = Stream.of(
Block.createCuboidShape(7, 2, 4, 9, 4, 5),
Expand Down Expand Up @@ -127,7 +128,7 @@ public void onLandedUpon(World world, BlockState state, BlockPos pos, Entity ent
}

@Override
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
if (world.getBlockEntity(pos) instanceof PiggyBankBlockEntity piggyBank && player.isCreative() && !world.isClient && !piggyBank.inventory().stream().allMatch(ItemStack::isEmpty)) {

var stack = new ItemStack(NumismaticOverhaulBlocks.PIGGY_BANK);
Expand All @@ -138,7 +139,7 @@ public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity pl
world.spawnEntity(var);
}

super.onBreak(world, pos, state, player);
return super.onBreak(world, pos, state, player);
}

@Override
Expand Down Expand Up @@ -168,4 +169,9 @@ public List<ItemStack> getDroppedStacks(BlockState state, LootContextParameterSe
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new PiggyBankBlockEntity(pos, state);
}

@Override
protected MapCodec<? extends HorizontalFacingBlock> getCodec() {
return CODEC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.glisco.numismaticoverhaul.NumismaticOverhaul;
import com.glisco.numismaticoverhaul.currency.CurrencyConverter;
import com.glisco.numismaticoverhaul.network.UpdateShopScreenS2CPacket;
import com.mojang.serialization.MapCodec;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
Expand All @@ -25,7 +26,6 @@
import org.jetbrains.annotations.Nullable;

public class ShopBlock extends BlockWithEntity {

private static final VoxelShape MAIN_PILLAR = Block.createCuboidShape(1, 0, 1, 14, 8, 14);

private static final VoxelShape PLATE = Block.createCuboidShape(0, 8, 0, 16, 12, 16);
Expand All @@ -44,6 +44,11 @@ public ShopBlock(boolean inexhaustible) {
this.inexhaustible = inexhaustible;
}

@Override
protected MapCodec<? extends BlockWithEntity> getCodec() {
return null; // TODO
}

@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.glisco.numismaticoverhaul.NumismaticOverhaul;
import io.wispforest.owo.ops.WorldOps;
import io.wispforest.owo.serialization.endec.KeyedEndec;
import io.wispforest.owo.util.ImplementedInventory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down Expand Up @@ -36,12 +37,13 @@ public class ShopBlockEntity extends LockableContainerBlockEntity implements Imp

private static final int[] SLOTS = IntStream.range(0, 27).toArray();
private static final int[] NO_SLOTS = new int[0];
public static KeyedEndec<List<ShopOffer>> OFFERS_LIST = ShopOffer.ENDEC.listOf().keyed("offers", ArrayList::new);

private final DefaultedList<ItemStack> INVENTORY = DefaultedList.ofSize(27, ItemStack.EMPTY);

public boolean busy = false;
private final Merchant merchant;
private final List<ShopOffer> offers;
private List<ShopOffer> offers;

private long storedCurrency;
private UUID owner;
Expand Down Expand Up @@ -119,7 +121,7 @@ public void addCurrency(long value) {
public void writeNbt(NbtCompound tag) {
super.writeNbt(tag);
Inventories.writeNbt(tag, INVENTORY);
ShopOffer.writeAll(tag, offers);
tag.put(OFFERS_LIST, offers);
tag.putBoolean("AllowsTransfer", this.allowsTransfer);
tag.putLong("StoredCurrency", storedCurrency);
if (owner != null) {
Expand All @@ -131,7 +133,7 @@ public void writeNbt(NbtCompound tag) {
public void readNbt(NbtCompound tag) {
super.readNbt(tag);
Inventories.readNbt(tag, INVENTORY);
ShopOffer.readAll(tag, offers);
this.offers = tag.get(OFFERS_LIST);
if (tag.contains("Owner")) {
owner = tag.getUuid("Owner");
}
Expand Down Expand Up @@ -171,11 +173,11 @@ public void deleteOffer(ItemStack stack) {
this.markDirty();
}

public static void tick(World world, BlockPos pos, BlockState state, ShopBlockEntity blockEntity) {
blockEntity.tick();
public static void tick(World world, BlockPos ignoredPos, BlockState ignoredState, ShopBlockEntity blockEntity) {
blockEntity.tick(world);
}

public void tick() {
public void tick(World world) {
if (world.getTime() % 60 == 0) tradeIndex++;
}

Expand Down
51 changes: 7 additions & 44 deletions src/main/java/com/glisco/numismaticoverhaul/block/ShopOffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import com.glisco.numismaticoverhaul.currency.CurrencyConverter;
import com.glisco.numismaticoverhaul.item.MoneyBagItem;
import com.glisco.numismaticoverhaul.villagers.data.NumismaticTradeOfferExtensions;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.village.TradeOffer;

import java.util.List;

public class ShopOffer {
public static final Endec<ShopOffer> ENDEC = StructEndecBuilder.of(
BuiltInEndecs.ITEM_STACK.fieldOf("sell", ShopOffer::getSellStack),
Endec.LONG.fieldOf("price", ShopOffer::getPrice),
ShopOffer::new
);

private final ItemStack sell;
private final long price;
Expand Down Expand Up @@ -44,45 +46,6 @@ public ItemStack getSellStack() {
return sell.copy();
}

public static NbtCompound writeAll(NbtCompound tag, List<ShopOffer> offers) {

NbtList offerList = new NbtList();

for (ShopOffer offer : offers) {
offerList.add(offer.toNbt());
}

tag.put("Offers", offerList);

return tag;
}

public static void readAll(NbtCompound tag, List<ShopOffer> offers) {
offers.clear();

NbtList offerList = tag.getList("Offers", NbtElement.COMPOUND_TYPE);

for (NbtElement offerTag : offerList) {
offers.add(fromNbt((NbtCompound) offerTag));
}
}

public NbtCompound toNbt() {
var nbt = new NbtCompound();
nbt.putLong("Price", this.price);

var itemNbt = new NbtCompound();
this.sell.writeNbt(itemNbt);

nbt.put("Item", itemNbt);
return nbt;
}

public static ShopOffer fromNbt(NbtCompound nbt) {
var item = ItemStack.fromNbt(nbt.getCompound("Item"));
return new ShopOffer(item, nbt.getLong("Price"));
}

public static int count(DefaultedList<ItemStack> stacks, ItemStack testStack) {
int count = 0;
for (var stack : stacks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public class CurrencyComponent implements Component, AutoSyncedComponent {

public CurrencyComponent(PlayerEntity provider) {
this.provider = provider;
this.transactions = new ArrayList<Long>();
this.transactions = new ArrayList<>();
}

@Override
public void readFromNbt(NbtCompound tag) {
value = tag.getLong("Value");
value = tag.get(CurrencyHelper.VALUE);
}

@Override
public void writeToNbt(NbtCompound tag) {
tag.putLong("Value", value);
tag.put(CurrencyHelper.VALUE, value);
}

public long getValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import com.glisco.numismaticoverhaul.item.CoinItem;
import com.glisco.numismaticoverhaul.item.CurrencyItem;
import com.glisco.numismaticoverhaul.item.MoneyBagItem;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.BuiltInEndecs;
import io.wispforest.owo.serialization.endec.KeyedEndec;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;

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

public class CurrencyHelper {

public static final KeyedEndec<Long> VALUE = new KeyedEndec<>("Value", Endec.LONG, 0L);
public static final KeyedEndec<long[]> VALUES = new KeyedEndec<>("Values", BuiltInEndecs.LONG_ARRAY, new long[]{});

/**
* Checks how much money a player has as coin items in their inventory
*
Expand Down Expand Up @@ -97,21 +101,11 @@ public static ItemStack getClosest(long value) {
return CurrencyConverter.getAsItemStackList(CurrencyResolver.combineValues(values)).get(0);
}

public static long[] getFromNbt(NbtCompound nbt, String key) {
if (nbt.contains(key, NbtElement.LONG_ARRAY_TYPE)) return nbt.getLongArray(key).clone();
if (!nbt.contains(key, NbtElement.INT_ARRAY_TYPE)) return new long[0];

var intArray = nbt.getIntArray(key);
var longArray = new long[intArray.length];
for (int i = 0; i < intArray.length; i++) {
longArray[i] = intArray[i];
}

return longArray;
}

private static boolean isCombined(ItemStack stack) {
return stack.hasNbt() && stack.getNbt().contains("Combined", NbtElement.BYTE_TYPE);
return stack.get(MoneyBagItem.COMBINED);
}

public static long[] getValues(ItemStack stack) {
return stack.get(VALUES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.loot.entry.LeafEntry;
import net.minecraft.loot.entry.LootPoolEntryType;
import net.minecraft.loot.function.LootFunction;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.dynamic.Codecs;
import net.minecraft.util.math.MathHelper;

Expand All @@ -25,7 +24,7 @@ public class MoneyBagLootEntry extends LeafEntry {
public static final Codec<MoneyBagLootEntry> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codecs.createStrictOptionalFieldCodec(Codec.INT, "min", 0).forGetter(o -> o.min),
Codec.INT.fieldOf("max").forGetter(o -> o.max)
).and(method_53290(instance)).apply(instance, MoneyBagLootEntry::new));
).and(LeafEntry.addLeafFields(instance)).apply(instance, MoneyBagLootEntry::new));

private final int min;
private final int max;
Expand Down
Loading

0 comments on commit b5372f8

Please sign in to comment.