Skip to content

Commit

Permalink
Preliminary Lootr compatibility for Amphoras
Browse files Browse the repository at this point in the history
TODO: ConfigManagerMixin, textures

Signed-off-by: unilock <[email protected]>
  • Loading branch information
unilock committed Dec 21, 2024
1 parent dfe74bb commit e46aef4
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dependencies {
// modCompileOnly("maven.modrinth:enchantment-descriptions:${project.enchantment_descriptions_version}")
modCompileOnly("maven.modrinth:travelersbackpack:${project.travelers_backpack_version}")
modCompileOnly("maven.modrinth:botania:${project.botania_version}")
modCompileOnly("maven.modrinth:lootr:${project.lootr_version}")
modImplementation("maven.modrinth:idwtialsimmoedm:${project.idwtialsimmoedm_version}")

//Porting Lib
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ port_lib_version = 2.3.5+1.20.1
port_lib_modules = lazy_registration
# https://modrinth.com/mod/exclusions-lib
exclusionslib_version=0.6
# https://modrinth.com/mod/lootr/versions
lootr_version=0.7.35.85

# What recipe viewer to use ('emi', 'rei', or 'disabled')
recipe_viewer=emi
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,21 @@ protected void onViewerCountUpdate(World world, BlockPos pos, BlockState state,

@Override
protected boolean isPlayerViewing(PlayerEntity player) {
if (player.currentScreenHandler instanceof GenericContainerScreenHandler) {
Inventory inventory = ((GenericContainerScreenHandler)player.currentScreenHandler).getInventory();
return inventory == AmphoraBlockEntity.this;
} else {
return false;
}
return AmphoraBlockEntity.this.isPlayerViewing(player);
}
};
}

// This is a separate method to make Lootr integration easier
protected boolean isPlayerViewing(PlayerEntity player) {
if (player.currentScreenHandler instanceof GenericContainerScreenHandler) {
Inventory inventory = ((GenericContainerScreenHandler)player.currentScreenHandler).getInventory();
return inventory == this;
} else {
return false;
}
}

@Override
protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.dafuqs.spectrum.compat.exclusions_lib.*;
import de.dafuqs.spectrum.compat.farmersdelight.*;
import de.dafuqs.spectrum.compat.gobber.*;
import de.dafuqs.spectrum.compat.lootr.*;
import de.dafuqs.spectrum.compat.malum.*;
import de.dafuqs.spectrum.compat.modonomicon.*;
import de.dafuqs.spectrum.compat.neepmeat.*;
Expand Down Expand Up @@ -47,6 +48,7 @@ protected static void registerIntegrationPack(String modId, Supplier<ModIntegrat
public static final String NEEPMEAT_ID = "neepmeat";
public static final String MALUM_ID = "malum";
public static final String EXCLUSIONS_LIB_ID = "exclusions_lib";
public static final String LOOTR_ID = "lootr";

@SuppressWarnings("Convert2MethodRef")
public static void register() {
Expand All @@ -68,6 +70,7 @@ public static void register() {
registerIntegrationPack(MALUM_ID, () -> new MalumCompat());
registerIntegrationPack(TRAVELERS_BACKPACK_ID, () -> new TravelersBackpackCompat());
registerIntegrationPack(CREATE_ID, () -> new CreateCompat());
registerIntegrationPack(LOOTR_ID, () -> new LootrCompat());
}

for (ModIntegrationPack container : INTEGRATION_PACKS.values()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.dafuqs.spectrum.compat.lootr;

import de.dafuqs.spectrum.blocks.amphora.*;
import net.minecraft.block.*;
import net.minecraft.block.entity.*;
import net.minecraft.entity.player.*;
import net.minecraft.util.*;
import net.minecraft.util.hit.*;
import net.minecraft.util.math.*;
import net.minecraft.world.*;
import net.zestyblaze.lootr.api.*;
import net.zestyblaze.lootr.util.*;
import org.jetbrains.annotations.*;

public class LootAmphoraBlock extends AmphoraBlock {
public LootAmphoraBlock(Settings settings) {
super(settings);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (player.isSneaking()) {
ChestUtil.handleLootSneak(this, world, pos, player);
} else {
ChestUtil.handleLootChest(this, world, pos, player);
}
return ActionResult.SUCCESS;
}

@Override
public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new LootAmphoraBlockEntity(pos, state);
}

@Override
public float calcBlockBreakingDelta(BlockState state, PlayerEntity player, BlockView world, BlockPos pos) {
return LootrAPI.getDestroyProgress(state, player, world, pos, super.calcBlockBreakingDelta(state, player, world, pos));
}

@Override
public float getBlastResistance() {
return LootrAPI.getExplosionResistance(this, super.getBlastResistance());
}

@Override
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return LootrAPI.getAnalogOutputSignal(state, world, pos, super.getComparatorOutput(state, world, pos));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package de.dafuqs.spectrum.compat.lootr;

import com.google.common.collect.*;
import de.dafuqs.spectrum.blocks.amphora.*;
import net.minecraft.advancement.criterion.*;
import net.minecraft.block.*;
import net.minecraft.block.entity.*;
import net.minecraft.entity.player.*;
import net.minecraft.inventory.*;
import net.minecraft.item.*;
import net.minecraft.loot.*;
import net.minecraft.loot.context.*;
import net.minecraft.nbt.*;
import net.minecraft.screen.*;
import net.minecraft.server.network.*;
import net.minecraft.server.world.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
import net.minecraft.util.collection.*;
import net.minecraft.util.math.*;
import net.zestyblaze.lootr.api.*;
import net.zestyblaze.lootr.api.blockentity.*;
import net.zestyblaze.lootr.client.*;
import net.zestyblaze.lootr.config.*;
import net.zestyblaze.lootr.data.*;
import org.jetbrains.annotations.*;

import java.util.*;

public class LootAmphoraBlockEntity extends AmphoraBlockEntity implements ILootBlockEntity {
public Set<UUID> openers = new HashSet<>();
protected Identifier savedLootTable = null;
protected long seed = -1L;
protected UUID tileId = null;
protected boolean opened = false;
private boolean savingToItem = false;

public LootAmphoraBlockEntity(BlockPos pos, BlockState state) {
super(pos, state);
}

// We cannot set the BlockEntityType in the constructor, so hopefully doing it here is enough
// (the field in BlockEntity is private anyway)
@Override
public BlockEntityType<?> getType() {
return LootrCompat.LOOT_AMPHORA;
}

@Override
protected boolean isPlayerViewing(PlayerEntity player) {
if (player.currentScreenHandler instanceof GenericContainerScreenHandler) {
Inventory inventory = ((GenericContainerScreenHandler) player.currentScreenHandler).getInventory();
if (inventory instanceof SpecialChestInventory data) {
if (data.getTileId() == null) {
return data.getBlockEntity(this.world) == this;
}

return data.getTileId().equals(this.getTileId());
}
}

return false;
}

@Override
protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);

if (this.savedLootTable != null) {
nbt.putString("LootTable", this.savedLootTable.toString());
}

if (this.seed != -1L) {
nbt.putLong("LootTableSeed", this.seed);
}

if (!LootrAPI.shouldDiscard() && !this.savingToItem) {
nbt.putUuid("tileId", this.getTileId());
NbtList list = new NbtList();

for (UUID opener : this.openers) {
list.add(NbtHelper.fromUuid(opener));
}

nbt.put("LootrOpeners", list);
}
}

@Override
public void readNbt(NbtCompound nbt) {
if (nbt.contains("specialLootChest_table", NbtElement.STRING_TYPE)) {
this.savedLootTable = new Identifier(nbt.getString("specialLootChest_table"));
}

if (nbt.contains("specialLootChest_seed", NbtElement.LONG_TYPE)) {
this.seed = nbt.getLong("specialLootChest_seed");
}

if (this.savedLootTable == null && nbt.contains("LootTable", NbtElement.STRING_TYPE)) {
this.savedLootTable = new Identifier(nbt.getString("LootTable"));
if (nbt.contains("LootTableSeed", NbtElement.LONG_TYPE)) {
this.seed = nbt.getLong("LootTableSeed");
}

this.setLootTable(this.savedLootTable, this.seed);
}

if (nbt.containsUuid("tileId")) {
this.tileId = nbt.getUuid("tileId");
}

if (this.tileId == null) {
this.getTileId();
}

if (nbt.contains("LootrOpeners")) {
Set<UUID> newOpeners = new HashSet<>();

for (NbtElement item : nbt.getList("LootrOpeners", NbtElement.INT_ARRAY_TYPE)) {
newOpeners.add(NbtHelper.toUuid(item));
}

if (!Sets.symmetricDifference(this.openers, newOpeners).isEmpty()) {
this.openers = newOpeners;
if (this.getWorld() != null && this.getWorld().isClient()) {
ClientHooks.clearCache(this.getPos());
}
}
}

super.readNbt(nbt);
}

@Override
protected void setInvStackList(DefaultedList<ItemStack> list) {
}

@Override
protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) {
return null;
}

@Override
public void setStackNbt(ItemStack stack) {
this.savingToItem = true;
super.setStackNbt(stack);
this.savingToItem = false;
}

@Override
public void checkLootInteraction(@Nullable PlayerEntity player) {
}

@Override
public void setLootTable(Identifier id, long seed) {
this.savedLootTable = id;
this.seed = seed;
super.setLootTable(id, seed);
}

@Override
public NbtCompound toInitialChunkDataNbt() {
NbtCompound nbt = super.toInitialChunkDataNbt();
this.writeNbt(nbt);
return nbt;
}

@Override
public Set<UUID> getOpeners() {
return this.openers;
}

@Override
public BlockPos getPosition() {
return this.getPos();
}

@Override
public long getSeed() {
return this.seed;
}

@Override
public Identifier getTable() {
return this.savedLootTable;
}

@Override
public UUID getTileId() {
if (this.tileId == null) {
this.tileId = UUID.randomUUID();
}

return this.tileId;
}

@Override
public void setOpened(boolean opened) {
this.opened = opened;
}

@Override
public void unpackLootTable(PlayerEntity playerEntity, Inventory inventory, Identifier identifier, long seed) {
if (this.world != null && this.savedLootTable != null && this.world.getServer() != null) {
LootTable loottable = this.world.getServer().getLootManager().getLootTable(identifier != null ? identifier : this.savedLootTable);
if (loottable == LootTable.EMPTY) {
LootrAPI.LOG.error("Unable to fill loot amphora in " + this.world.getRegistryKey() + " at " + this.pos + " as the loot table '" + (identifier != null ? identifier : this.savedLootTable) + "' couldn't be resolved! Please search the loot table in `latest.log` to see if there are errors in loading.");
if (ConfigManager.get().debug.report_invalid_tables) {
playerEntity.sendMessage(Text.translatable("lootr.message.invalid_table", (identifier != null ? identifier : this.savedLootTable).toString()).setStyle(ConfigManager.get().notifications.disable_message_styles ? Style.EMPTY : Style.EMPTY.withColor(TextColor.fromFormatting(Formatting.DARK_RED)).withBold(true)), false);
}
}

if (playerEntity instanceof ServerPlayerEntity serverPlayerEntity) {
Criteria.PLAYER_GENERATES_CONTAINER_LOOT.trigger(serverPlayerEntity, identifier != null ? identifier : this.lootTableId);
}

LootContextParameterSet.Builder builder = (new LootContextParameterSet.Builder((ServerWorld) this.world)).add(LootContextParameters.ORIGIN, Vec3d.ofCenter(this.pos));
if (playerEntity != null) {
builder.luck(playerEntity.getLuck()).add(LootContextParameters.THIS_ENTITY, playerEntity);
}

loottable.supplyInventory(inventory, builder.build(LootContextTypes.CHEST), LootrAPI.getLootSeed(seed == Long.MIN_VALUE ? this.seed : seed));
}
}

@Override
public void updatePacketViaState() {
if (this.world != null && !this.world.isClient) {
BlockState state = this.world.getBlockState(this.getPos());
this.world.updateListeners(this.getPos(), state, state, 8);
}
}

// @Override
// public @Nullable Object getRenderAttachmentData() {
// PlayerEntity player = ClientHooks.getPlayer();
// return player == null ? null : this.getOpeners().contains(player.getUuid());
// }
}
33 changes: 33 additions & 0 deletions src/main/java/de/dafuqs/spectrum/compat/lootr/LootrCompat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.dafuqs.spectrum.compat.lootr;

import de.dafuqs.spectrum.compat.*;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.block.*;
import net.minecraft.block.entity.*;
import net.minecraft.util.*;

public class LootrCompat extends SpectrumIntegrationPacks.ModIntegrationPack {
public static final Block SLATE_NOXWOOD_LOOT_AMPHORA = new LootAmphoraBlock(AbstractBlock.Settings.copy(SpectrumBlocks.SLATE_NOXWOOD_AMPHORA));
public static final Block EBONY_NOXWOOD_LOOT_AMPHORA = new LootAmphoraBlock(AbstractBlock.Settings.copy(SpectrumBlocks.EBONY_NOXWOOD_AMPHORA));
public static final Block IVORY_NOXWOOD_LOOT_AMPHORA = new LootAmphoraBlock(AbstractBlock.Settings.copy(SpectrumBlocks.IVORY_NOXWOOD_AMPHORA));
public static final Block CHESTNUT_NOXWOOD_LOOT_AMPHORA = new LootAmphoraBlock(AbstractBlock.Settings.copy(SpectrumBlocks.CHESTNUT_NOXWOOD_AMPHORA));
public static final Block WEEPING_GALA_LOOT_AMPHORA = new LootAmphoraBlock(AbstractBlock.Settings.copy(SpectrumBlocks.WEEPING_GALA_AMPHORA));

public static BlockEntityType<LootAmphoraBlockEntity> LOOT_AMPHORA;

@Override
public void register() {
SpectrumBlocks.registerBlockWithItem("slate_noxwood_loot_amphora", SLATE_NOXWOOD_LOOT_AMPHORA, SpectrumItems.IS.of(), DyeColor.LIME);
SpectrumBlocks.registerBlockWithItem("ebony_noxwood_loot_amphora", EBONY_NOXWOOD_LOOT_AMPHORA, SpectrumItems.IS.of(), DyeColor.LIME);
SpectrumBlocks.registerBlockWithItem("ivory_noxwood_loot_amphora", IVORY_NOXWOOD_LOOT_AMPHORA, SpectrumItems.IS.of(), DyeColor.LIME);
SpectrumBlocks.registerBlockWithItem("chestnut_noxwood_loot_amphora", CHESTNUT_NOXWOOD_LOOT_AMPHORA, SpectrumItems.IS.of(), DyeColor.LIME);
SpectrumBlocks.registerBlockWithItem("weeping_gala_loot_amphora", WEEPING_GALA_LOOT_AMPHORA, SpectrumItems.IS.of(), DyeColor.LIME);

LOOT_AMPHORA = SpectrumBlockEntities.register("loot_amphora", LootAmphoraBlockEntity::new, SLATE_NOXWOOD_LOOT_AMPHORA, EBONY_NOXWOOD_LOOT_AMPHORA, IVORY_NOXWOOD_LOOT_AMPHORA, CHESTNUT_NOXWOOD_LOOT_AMPHORA, WEEPING_GALA_LOOT_AMPHORA);
}

@Override
public void registerClient() {

}
}
Loading

0 comments on commit e46aef4

Please sign in to comment.