Skip to content

Commit

Permalink
Rewrite loot in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Jul 27, 2024
1 parent 904da0d commit 73e13f0
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package juuxel.adorn.loot;

import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import juuxel.adorn.lib.registry.RegistrarFactory;
import net.minecraft.loot.condition.LootConditionType;
import net.minecraft.registry.RegistryKeys;

public final class AdornLootConditionTypes {
public static final Registrar<LootConditionType> LOOT_CONDITION_TYPES = RegistrarFactory.get().create(RegistryKeys.LOOT_CONDITION_TYPE);
public static final Registered<LootConditionType> GAME_RULE =
LOOT_CONDITION_TYPES.register("game_rule", () -> new LootConditionType(GameRuleLootCondition.CODEC));

public static void init() {
}
}
16 changes: 16 additions & 0 deletions common/src/main/java/juuxel/adorn/loot/AdornLootFunctionTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package juuxel.adorn.loot;

import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import juuxel.adorn.lib.registry.RegistrarFactory;
import net.minecraft.loot.function.LootFunctionType;
import net.minecraft.registry.RegistryKeys;

public final class AdornLootFunctionTypes {
public static final Registrar<LootFunctionType> LOOT_FUNCTION_TYPES = RegistrarFactory.get().create(RegistryKeys.LOOT_FUNCTION_TYPE);
public static final Registered<LootFunctionType> CHECK_TRADING_STATION_OWNER =
LOOT_FUNCTION_TYPES.register("check_trading_station_owner", () -> new LootFunctionType(CheckTradingStationOwnerLootFunction.CODEC));

public static void init() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package juuxel.adorn.loot;

import com.mojang.serialization.Codec;
import juuxel.adorn.block.AdornBlocks;
import juuxel.adorn.block.entity.TradingStationBlockEntity;
import juuxel.adorn.trading.Trade;
import juuxel.adorn.util.InventoryComponent;
import juuxel.adorn.util.NbtExtensionsKt;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.function.LootFunction;
import net.minecraft.loot.function.LootFunctionType;
import net.minecraft.nbt.NbtCompound;

public final class CheckTradingStationOwnerLootFunction implements LootFunction {
public static final CheckTradingStationOwnerLootFunction INSTANCE = new CheckTradingStationOwnerLootFunction();
public static final Codec<CheckTradingStationOwnerLootFunction> CODEC = Codec.unit(INSTANCE);

private CheckTradingStationOwnerLootFunction() {
}

@Override
public ItemStack apply(ItemStack stack, LootContext lootContext) {
if (stack.isOf(AdornBlocks.INSTANCE.getTRADING_STATION().asItem())) {
var blockEntityNbt = stack.getSubNbt(BlockItem.BLOCK_ENTITY_TAG_KEY);
if (blockEntityNbt != null) {
if (!hasTrade(blockEntityNbt) || !hasStorage(blockEntityNbt)) {
clearOwner(blockEntityNbt);
}
}
}

return stack;
}

private boolean hasTrade(NbtCompound nbt) {
var tradeNbt = NbtExtensionsKt.getCompoundOrNull(nbt, TradingStationBlockEntity.NBT_TRADE);
if (tradeNbt == null) return false;
var trade = Trade.fromNbt(tradeNbt);
return !trade.isFullyEmpty();
}

private boolean hasStorage(NbtCompound nbt) {
var storageNbt = NbtExtensionsKt.getCompoundOrNull(nbt, TradingStationBlockEntity.NBT_STORAGE);
if (storageNbt == null) return false;
var inventory = new InventoryComponent(TradingStationBlockEntity.STORAGE_SIZE);
inventory.readNbt(storageNbt);
return !inventory.isEmpty();
}

private void clearOwner(NbtCompound nbt) {
nbt.remove(TradingStationBlockEntity.NBT_TRADING_OWNER);
nbt.remove(TradingStationBlockEntity.NBT_TRADING_OWNER_NAME);
}

@Override
public LootFunctionType getType() {
return AdornLootFunctionTypes.CHECK_TRADING_STATION_OWNER.get();
}
}
40 changes: 40 additions & 0 deletions common/src/main/java/juuxel/adorn/loot/GameRuleLootCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package juuxel.adorn.loot;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import juuxel.adorn.util.Logging;
import net.minecraft.loot.condition.LootCondition;
import net.minecraft.loot.condition.LootConditionType;
import net.minecraft.loot.context.LootContext;
import net.minecraft.world.GameRules;
import org.slf4j.Logger;

public record GameRuleLootCondition(GameRules.Key<?> gameRule) implements LootCondition {
public static final Codec<GameRuleLootCondition> CODEC = RecordCodecBuilder.create(builder -> builder.group(
Codec.STRING
.<GameRules.Key<?>>xmap(name -> new GameRules.Key<>(name, GameRules.Category.MISC), GameRules.Key::getName)
.fieldOf("game_rule")
.forGetter(GameRuleLootCondition::gameRule)
).apply(builder, GameRuleLootCondition::new));
private static final Logger LOGGER = Logging.logger();

@Override
public boolean test(LootContext lootContext) {
var rule = lootContext.getWorld().getGameRules().get(gameRule);

if (rule instanceof GameRules.BooleanRule booleanRule) {
return booleanRule.get();
} else if (rule == null) {
LOGGER.error("Unknown game rule {} in loot condition", gameRule);
} else {
LOGGER.error("Game rule {} ({}) is not a boolean", rule, gameRule);
}

return false;
}

@Override
public LootConditionType getType() {
return AdornLootConditionTypes.GAME_RULE.get();
}
}

This file was deleted.

16 changes: 0 additions & 16 deletions common/src/main/kotlin/juuxel/adorn/loot/AdornLootFunctionTypes.kt

This file was deleted.

This file was deleted.

41 changes: 0 additions & 41 deletions common/src/main/kotlin/juuxel/adorn/loot/GameRuleLootCondition.kt

This file was deleted.

1 change: 1 addition & 0 deletions common/src/main/kotlin/juuxel/adorn/trading/Trade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data class Trade(var selling: ItemStack, var price: ItemStack) : NbtConvertible,
fun empty(): Trade =
Trade(ItemStack.EMPTY, ItemStack.EMPTY)

@JvmStatic
fun fromNbt(nbt: NbtCompound): Trade {
val trade = empty()
trade.readNbt(nbt)
Expand Down
1 change: 1 addition & 0 deletions common/src/main/kotlin/juuxel/adorn/util/Logging.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@file:JvmName("Logging")
package juuxel.adorn.util

import org.slf4j.Logger
Expand Down

0 comments on commit 73e13f0

Please sign in to comment.