Skip to content

Commit 73e13f0

Browse files
committed
Rewrite loot in Java
1 parent 904da0d commit 73e13f0

10 files changed

+135
-122
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package juuxel.adorn.loot;
2+
3+
import juuxel.adorn.lib.registry.Registered;
4+
import juuxel.adorn.lib.registry.Registrar;
5+
import juuxel.adorn.lib.registry.RegistrarFactory;
6+
import net.minecraft.loot.condition.LootConditionType;
7+
import net.minecraft.registry.RegistryKeys;
8+
9+
public final class AdornLootConditionTypes {
10+
public static final Registrar<LootConditionType> LOOT_CONDITION_TYPES = RegistrarFactory.get().create(RegistryKeys.LOOT_CONDITION_TYPE);
11+
public static final Registered<LootConditionType> GAME_RULE =
12+
LOOT_CONDITION_TYPES.register("game_rule", () -> new LootConditionType(GameRuleLootCondition.CODEC));
13+
14+
public static void init() {
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package juuxel.adorn.loot;
2+
3+
import juuxel.adorn.lib.registry.Registered;
4+
import juuxel.adorn.lib.registry.Registrar;
5+
import juuxel.adorn.lib.registry.RegistrarFactory;
6+
import net.minecraft.loot.function.LootFunctionType;
7+
import net.minecraft.registry.RegistryKeys;
8+
9+
public final class AdornLootFunctionTypes {
10+
public static final Registrar<LootFunctionType> LOOT_FUNCTION_TYPES = RegistrarFactory.get().create(RegistryKeys.LOOT_FUNCTION_TYPE);
11+
public static final Registered<LootFunctionType> CHECK_TRADING_STATION_OWNER =
12+
LOOT_FUNCTION_TYPES.register("check_trading_station_owner", () -> new LootFunctionType(CheckTradingStationOwnerLootFunction.CODEC));
13+
14+
public static void init() {
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package juuxel.adorn.loot;
2+
3+
import com.mojang.serialization.Codec;
4+
import juuxel.adorn.block.AdornBlocks;
5+
import juuxel.adorn.block.entity.TradingStationBlockEntity;
6+
import juuxel.adorn.trading.Trade;
7+
import juuxel.adorn.util.InventoryComponent;
8+
import juuxel.adorn.util.NbtExtensionsKt;
9+
import net.minecraft.item.BlockItem;
10+
import net.minecraft.item.ItemStack;
11+
import net.minecraft.loot.context.LootContext;
12+
import net.minecraft.loot.function.LootFunction;
13+
import net.minecraft.loot.function.LootFunctionType;
14+
import net.minecraft.nbt.NbtCompound;
15+
16+
public final class CheckTradingStationOwnerLootFunction implements LootFunction {
17+
public static final CheckTradingStationOwnerLootFunction INSTANCE = new CheckTradingStationOwnerLootFunction();
18+
public static final Codec<CheckTradingStationOwnerLootFunction> CODEC = Codec.unit(INSTANCE);
19+
20+
private CheckTradingStationOwnerLootFunction() {
21+
}
22+
23+
@Override
24+
public ItemStack apply(ItemStack stack, LootContext lootContext) {
25+
if (stack.isOf(AdornBlocks.INSTANCE.getTRADING_STATION().asItem())) {
26+
var blockEntityNbt = stack.getSubNbt(BlockItem.BLOCK_ENTITY_TAG_KEY);
27+
if (blockEntityNbt != null) {
28+
if (!hasTrade(blockEntityNbt) || !hasStorage(blockEntityNbt)) {
29+
clearOwner(blockEntityNbt);
30+
}
31+
}
32+
}
33+
34+
return stack;
35+
}
36+
37+
private boolean hasTrade(NbtCompound nbt) {
38+
var tradeNbt = NbtExtensionsKt.getCompoundOrNull(nbt, TradingStationBlockEntity.NBT_TRADE);
39+
if (tradeNbt == null) return false;
40+
var trade = Trade.fromNbt(tradeNbt);
41+
return !trade.isFullyEmpty();
42+
}
43+
44+
private boolean hasStorage(NbtCompound nbt) {
45+
var storageNbt = NbtExtensionsKt.getCompoundOrNull(nbt, TradingStationBlockEntity.NBT_STORAGE);
46+
if (storageNbt == null) return false;
47+
var inventory = new InventoryComponent(TradingStationBlockEntity.STORAGE_SIZE);
48+
inventory.readNbt(storageNbt);
49+
return !inventory.isEmpty();
50+
}
51+
52+
private void clearOwner(NbtCompound nbt) {
53+
nbt.remove(TradingStationBlockEntity.NBT_TRADING_OWNER);
54+
nbt.remove(TradingStationBlockEntity.NBT_TRADING_OWNER_NAME);
55+
}
56+
57+
@Override
58+
public LootFunctionType getType() {
59+
return AdornLootFunctionTypes.CHECK_TRADING_STATION_OWNER.get();
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package juuxel.adorn.loot;
2+
3+
import com.mojang.serialization.Codec;
4+
import com.mojang.serialization.codecs.RecordCodecBuilder;
5+
import juuxel.adorn.util.Logging;
6+
import net.minecraft.loot.condition.LootCondition;
7+
import net.minecraft.loot.condition.LootConditionType;
8+
import net.minecraft.loot.context.LootContext;
9+
import net.minecraft.world.GameRules;
10+
import org.slf4j.Logger;
11+
12+
public record GameRuleLootCondition(GameRules.Key<?> gameRule) implements LootCondition {
13+
public static final Codec<GameRuleLootCondition> CODEC = RecordCodecBuilder.create(builder -> builder.group(
14+
Codec.STRING
15+
.<GameRules.Key<?>>xmap(name -> new GameRules.Key<>(name, GameRules.Category.MISC), GameRules.Key::getName)
16+
.fieldOf("game_rule")
17+
.forGetter(GameRuleLootCondition::gameRule)
18+
).apply(builder, GameRuleLootCondition::new));
19+
private static final Logger LOGGER = Logging.logger();
20+
21+
@Override
22+
public boolean test(LootContext lootContext) {
23+
var rule = lootContext.getWorld().getGameRules().get(gameRule);
24+
25+
if (rule instanceof GameRules.BooleanRule booleanRule) {
26+
return booleanRule.get();
27+
} else if (rule == null) {
28+
LOGGER.error("Unknown game rule {} in loot condition", gameRule);
29+
} else {
30+
LOGGER.error("Game rule {} ({}) is not a boolean", rule, gameRule);
31+
}
32+
33+
return false;
34+
}
35+
36+
@Override
37+
public LootConditionType getType() {
38+
return AdornLootConditionTypes.GAME_RULE.get();
39+
}
40+
}

common/src/main/kotlin/juuxel/adorn/loot/AdornLootConditionTypes.kt

-14
This file was deleted.

common/src/main/kotlin/juuxel/adorn/loot/AdornLootFunctionTypes.kt

-16
This file was deleted.

common/src/main/kotlin/juuxel/adorn/loot/CheckTradingStationOwnerLootFunction.kt

-51
This file was deleted.

common/src/main/kotlin/juuxel/adorn/loot/GameRuleLootCondition.kt

-41
This file was deleted.

common/src/main/kotlin/juuxel/adorn/trading/Trade.kt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ data class Trade(var selling: ItemStack, var price: ItemStack) : NbtConvertible,
4343
fun empty(): Trade =
4444
Trade(ItemStack.EMPTY, ItemStack.EMPTY)
4545

46+
@JvmStatic
4647
fun fromNbt(nbt: NbtCompound): Trade {
4748
val trade = empty()
4849
trade.readNbt(nbt)

common/src/main/kotlin/juuxel/adorn/util/Logging.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@file:JvmName("Logging")
12
package juuxel.adorn.util
23

34
import org.slf4j.Logger

0 commit comments

Comments
 (0)