-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
135 additions
and
122 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
common/src/main/java/juuxel/adorn/loot/AdornLootConditionTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
common/src/main/java/juuxel/adorn/loot/AdornLootFunctionTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
common/src/main/java/juuxel/adorn/loot/CheckTradingStationOwnerLootFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
common/src/main/java/juuxel/adorn/loot/GameRuleLootCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
common/src/main/kotlin/juuxel/adorn/loot/AdornLootConditionTypes.kt
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
common/src/main/kotlin/juuxel/adorn/loot/AdornLootFunctionTypes.kt
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
common/src/main/kotlin/juuxel/adorn/loot/CheckTradingStationOwnerLootFunction.kt
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
common/src/main/kotlin/juuxel/adorn/loot/GameRuleLootCondition.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|