Skip to content

Commit

Permalink
quick shulker compat
Browse files Browse the repository at this point in the history
  • Loading branch information
DaFuqs committed Oct 21, 2021
1 parent e2ffedf commit c525e83
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ private ArrayList<ItemStack> getPredefinedLootCrates() {
// no use in that tag when there is no lock, is there?
} else {
for(boolean relock : booleans) {
NbtCompound compound = LootCrateItem.getLootCrateItemCompoundTag(lootTable, locked, doNotConsumeKeyOnUnlock, replenishTimeTicks, 0, oncePerPlayer, relock, trapped);
ItemStack itemStack = new ItemStack(lootCrateItem);
itemStack.setNbt(compound);
stacks.add(itemStack);
if(replenishTimeTicks > 0) {
NbtCompound compound = LootCrateItem.getLootCrateItemCompoundTag(lootTable, locked, doNotConsumeKeyOnUnlock, replenishTimeTicks, 0, oncePerPlayer, relock, trapped);
ItemStack itemStack = new ItemStack(lootCrateItem);
itemStack.setNbt(compound);
stacks.add(itemStack);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
itemStack.decrement(1);
}
}
lootCrateBlockEntity.unlock();
lootCrateBlockEntity.unlock(world);
return ActionResult.CONSUME; // just consume the action and play unlock sound.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public abstract class LootCrateBlockEntity extends LootableContainerBlockEntity

private long replenishTimeTicks;
private long lastReplenishTimeTick;
private long lastUnlockTimeTick; // for the relock to not lock right back up

protected LootCrateBlockEntity(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) {
super(blockEntityType, blockPos, blockState);
Expand Down Expand Up @@ -115,7 +116,7 @@ protected void playSound(SoundEvent soundEvent, float volume) {
}
}

public boolean shouldGenerateNewLoot(PlayerEntity player, boolean rememberPlayer) {
public boolean shouldGenerateNewLoot(PlayerEntity player, boolean test) {
if(hasWorld()) {
// if replenish time is set to <=0: just generate loot once
if(this.replenishTimeTicks <= 0) {
Expand All @@ -133,7 +134,7 @@ public boolean shouldGenerateNewLoot(PlayerEntity player, boolean rememberPlayer
return false;
} else {
this.lastReplenishTimeTick = world.getTime();
if(rememberPlayer) {
if(!test) {
this.registeredPlayerUUIDs.add(player.getUuid());
this.markDirty();
}
Expand Down Expand Up @@ -189,6 +190,7 @@ public NbtCompound addLootCrateBlockTags(NbtCompound tag) {
}
if(this.relocksForNewLoot) {
tag.putBoolean(LootCrateTagNames.RelocksWhenNewLoot.toString(), true);
tag.putLong(LootCrateTagNames.LastUnlockTimeTick.toString(), this.lastUnlockTimeTick);
}
if(this.trapped) {
tag.putBoolean(LootCrateTagNames.Trapped.toString(), true);
Expand Down Expand Up @@ -223,6 +225,11 @@ public void setLootCrateBlockTags(NbtCompound tag) {
} else {
this.lastReplenishTimeTick = 0;
}
if(tag.contains(LootCrateTagNames.LastUnlockTimeTick.toString())) {
this.lastUnlockTimeTick = tag.getLong(LootCrateTagNames.LastUnlockTimeTick.toString());
} else {
this.lastUnlockTimeTick = 0;
}

this.locked = tag.contains(LootCrateTagNames.Locked.toString()) && tag.getBoolean(LootCrateTagNames.Locked.toString());
this.doNotConsumeKeyOnUnlock = tag.contains(LootCrateTagNames.DoNotConsumeKeyOnUnlock.toString()) && tag.getBoolean(LootCrateTagNames.DoNotConsumeKeyOnUnlock.toString());
Expand Down Expand Up @@ -262,7 +269,7 @@ protected Text getContainerName() {
}

public void checkRelock(PlayerEntity player) {
if(!this.locked && this.shouldGenerateNewLoot(player, false)) {
if(this.relocksForNewLoot && !this.locked && this.lastUnlockTimeTick < this.lastReplenishTimeTick && this.shouldGenerateNewLoot(player, true)) {
this.locked = true;
}
}
Expand Down Expand Up @@ -290,8 +297,9 @@ public boolean doesConsumeKeyOnUnlock() {
return !doNotConsumeKeyOnUnlock;
}

public void unlock() {
public void unlock(World world) {
this.locked = false;
this.lastUnlockTimeTick = world.getTime();
this.playSound(LootCrates.CHEST_UNLOCKS_SOUND_EVENT);
}

Expand Down
52 changes: 40 additions & 12 deletions src/main/java/de/dafuqs/lootcrates/compat/QuickShulkerCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ private void unlock(PlayerEntity player, ItemStack stack) {
NbtCompound tag = stack.getSubNbt("BlockEntityTag");
if (tag != null) {
tag.putBoolean(LootCrateTagNames.Locked.toString(), false);
tag.putLong(LootCrateTagNames.LastUnlockTimeTick.toString(), player.getEntityWorld().getTime());
}
}

public void checkRelock(PlayerEntity player, ItemStack stack) {
NbtCompound tag = stack.getSubNbt("BlockEntityTag");
if(tag.contains(LootCrateTagNames.RelocksWhenNewLoot.toString()) && tag.getBoolean(LootCrateTagNames.RelocksWhenNewLoot.toString())) {
if(tag.contains(LootCrateTagNames.Locked.toString()) && !tag.getBoolean(LootCrateTagNames.Locked.toString())) {
long lastUnlockTimeTick = 0;
long lastReplenishTimeTick = 0;
if(tag.contains(LootCrateTagNames.LastUnlockTimeTick.toString())) {
lastUnlockTimeTick = tag.getLong(LootCrateTagNames.LastUnlockTimeTick.toString());
}
if(tag.contains(LootCrateTagNames.LastReplenishTimeTick.toString())) {
lastReplenishTimeTick = tag.getLong(LootCrateTagNames.LastReplenishTimeTick.toString());
}
if(lastUnlockTimeTick < lastReplenishTimeTick && shouldGenerateNewLoot(stack, player, true)) {
tag.putBoolean(LootCrateTagNames.Locked.toString(), true);
}
}
}
}

Expand Down Expand Up @@ -123,7 +143,7 @@ private void checkLootInteraction(ItemStack stack, ServerPlayerEntity player) {
}

// only players can generate container loot
if (player != null && lootTableId != null && player.getServer() != null && shouldGenerateNewLoot(stack, player)) {
if (player != null && lootTableId != null && player.getServer() != null && shouldGenerateNewLoot(stack, player, false)) {
LootTable lootTable = player.getServer().getLootManager().getTable(lootTableId);

Criteria.PLAYER_GENERATES_CONTAINER_LOOT.test( player, lootTableId);
Expand All @@ -136,7 +156,7 @@ private void checkLootInteraction(ItemStack stack, ServerPlayerEntity player) {
}
}

public boolean shouldGenerateNewLoot(ItemStack stack, PlayerEntity player) {
public boolean shouldGenerateNewLoot(ItemStack stack, PlayerEntity player, boolean test) {
long replenishTimeTicks = -1;
long lastReplenishTimeTick = 0;
boolean oncePerPlayer = false;
Expand All @@ -151,10 +171,12 @@ public boolean shouldGenerateNewLoot(ItemStack stack, PlayerEntity player) {

if (tag.contains(LootCrateTagNames.OncePerPlayer.toString()) && tag.getBoolean(LootCrateTagNames.OncePerPlayer.toString())) {
oncePerPlayer = true;
if (tag.contains(LootCrateTagNames.RegisteredPlayerUUIDs.toString())) {
NbtList playerUUIDs = tag.getList(LootCrateTagNames.RegisteredPlayerUUIDs.toString(), 11);
for (NbtElement playerUUID : playerUUIDs) {
registeredPlayerUUIDs.add(NbtHelper.toUuid(playerUUID));
if(!test) {
if (tag.contains(LootCrateTagNames.RegisteredPlayerUUIDs.toString())) {
NbtList playerUUIDs = tag.getList(LootCrateTagNames.RegisteredPlayerUUIDs.toString(), 11);
for (NbtElement playerUUID : playerUUIDs) {
registeredPlayerUUIDs.add(NbtHelper.toUuid(playerUUID));
}
}
}
}
Expand All @@ -164,7 +186,9 @@ public boolean shouldGenerateNewLoot(ItemStack stack, PlayerEntity player) {
if (replenishTimeTicks <= 0) {
if (lastReplenishTimeTick == 0) {
lastReplenishTimeTick = player.world.getTime();
saveNbtToStack(stack, lastReplenishTimeTick, oncePerPlayer, registeredPlayerUUIDs);
if(!test) {
saveNbtToStack(stack, lastReplenishTimeTick, oncePerPlayer, registeredPlayerUUIDs);
}
return true;
} else {
return false;
Expand All @@ -176,14 +200,18 @@ public boolean shouldGenerateNewLoot(ItemStack stack, PlayerEntity player) {
if (registeredPlayerUUIDs.contains(player.getUuid())) {
return false;
} else {
lastReplenishTimeTick = player.world.getTime();
registeredPlayerUUIDs.add(player.getUuid());
saveNbtToStack(stack, lastReplenishTimeTick, oncePerPlayer, registeredPlayerUUIDs);
if(!test) {
lastReplenishTimeTick = player.world.getTime();
registeredPlayerUUIDs.add(player.getUuid());
saveNbtToStack(stack, lastReplenishTimeTick, oncePerPlayer, registeredPlayerUUIDs);
}
return true;
}
} else {
lastReplenishTimeTick = player.world.getTime();
saveNbtToStack(stack, lastReplenishTimeTick, oncePerPlayer, registeredPlayerUUIDs);
if(!test) {
lastReplenishTimeTick = player.world.getTime();
saveNbtToStack(stack, lastReplenishTimeTick, oncePerPlayer, registeredPlayerUUIDs);
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum LootCrateTagNames {
OncePerPlayer,
Trapped,
RegisteredPlayerUUIDs,
RelocksWhenNewLoot
RelocksWhenNewLoot,
LastUnlockTimeTick
}
4 changes: 4 additions & 0 deletions src/main/java/de/dafuqs/lootcrates/items/LootCrateItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip,
locked = true;
tooltip.add(LootCrateAtlas.getItemLockedTooltip(itemStack, compound));
}
if (compound.contains(LootCrateTagNames.RelocksWhenNewLoot.toString()) && compound.getBoolean(LootCrateTagNames.RelocksWhenNewLoot.toString())) {
tooltip.add(new TranslatableText("item.lootcrates.loot_crate.tooltip.relocks"));
}


if (compound.contains(LootCrateTagNames.Trapped.toString()) && compound.getBoolean(LootCrateTagNames.Trapped.toString())) {
tooltip.add(new TranslatableText("item.lootcrates.loot_crate.tooltip.trapped"));
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/lootcrates/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"item.lootcrates.loot_crate.tooltip.locked_consume_blaze": "Locked (unlocking will use up an §6Blazing Crate Key)",
"item.lootcrates.loot_crate.tooltip.already_looted": "§7Already looted",
"item.lootcrates.loot_crate.tooltip.trapped": "§e⚠ Trapped",
"item.lootcrates.loot_crate.tooltip.relocks": "§e⚠Will lock itself back up for new loot",
"item.lootcrates.loot_crate.tooltip.loot_table": "Loot Table: %s",
"item.lootcrates.loot_crate.tooltip.replenish_time_ticks": "Generates new loot every §o%d§r ticks",
"item.lootcrates.loot_crate.tooltip.replenish_time_minutes": "Generates new loot every §o%f§r minutes",
Expand Down

0 comments on commit c525e83

Please sign in to comment.