From 63055c935ac7ad0b5382d6cc5a609386e0fc7b49 Mon Sep 17 00:00:00 2001 From: DaFuqs Date: Tue, 14 Jun 2022 21:29:48 +0200 Subject: [PATCH] lots of code optimisations --- .../revelationary/ClientAdvancements.java | 2 +- .../revelationary/RevelationRegistry.java | 1 - .../api/advancements/AdvancementHelper.java | 2 +- .../api/revelations/CloakedBlock.java | 33 +++++++++++++++ .../api/revelations/CloakedBlockItem.java | 10 ++--- .../api/revelations/CloakedItem.java | 13 +----- .../api/revelations/RevelationAware.java | 41 ++----------------- .../mixin/AbstractBlockMixin.java | 30 +++----------- .../mixin/PlayerAdvancementTrackerMixin.java | 2 +- 9 files changed, 51 insertions(+), 83 deletions(-) create mode 100644 src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlock.java diff --git a/src/main/java/de/dafuqs/revelationary/ClientAdvancements.java b/src/main/java/de/dafuqs/revelationary/ClientAdvancements.java index bdd42c7..683eefd 100644 --- a/src/main/java/de/dafuqs/revelationary/ClientAdvancements.java +++ b/src/main/java/de/dafuqs/revelationary/ClientAdvancements.java @@ -19,7 +19,7 @@ public class ClientAdvancements { protected static boolean receivedFirstAdvancementPacket = false; - protected static List callbacks = new ArrayList<>(); + public static List callbacks = new ArrayList<>(); public static void onClientPacket(@NotNull AdvancementUpdateS2CPacket packet) { boolean hadPacketBefore = receivedFirstAdvancementPacket; diff --git a/src/main/java/de/dafuqs/revelationary/RevelationRegistry.java b/src/main/java/de/dafuqs/revelationary/RevelationRegistry.java index 8af5ba6..4be87c2 100644 --- a/src/main/java/de/dafuqs/revelationary/RevelationRegistry.java +++ b/src/main/java/de/dafuqs/revelationary/RevelationRegistry.java @@ -3,7 +3,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.brigadier.StringReader; -import de.dafuqs.revelationary.Revelationary; import de.dafuqs.revelationary.api.advancements.AdvancementHelper; import de.dafuqs.revelationary.api.revelations.RevelationAware; import net.minecraft.block.Block; diff --git a/src/main/java/de/dafuqs/revelationary/api/advancements/AdvancementHelper.java b/src/main/java/de/dafuqs/revelationary/api/advancements/AdvancementHelper.java index ecfdd08..5d90dfc 100644 --- a/src/main/java/de/dafuqs/revelationary/api/advancements/AdvancementHelper.java +++ b/src/main/java/de/dafuqs/revelationary/api/advancements/AdvancementHelper.java @@ -1,7 +1,7 @@ package de.dafuqs.revelationary.api.advancements; -import de.dafuqs.revelationary.Revelationary; import de.dafuqs.revelationary.ClientAdvancements; +import de.dafuqs.revelationary.Revelationary; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.advancement.Advancement; diff --git a/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlock.java b/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlock.java new file mode 100644 index 0000000..9bd380a --- /dev/null +++ b/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlock.java @@ -0,0 +1,33 @@ +package de.dafuqs.revelationary.api.revelations; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.item.Item; +import net.minecraft.util.Pair; + +import java.util.Hashtable; +import java.util.Map; + +public abstract class CloakedBlock extends Block implements RevelationAware { + + final Block cloakedBlock; + + public CloakedBlock(Settings settings, Block cloakedBlock) { + super(settings); + this.cloakedBlock = cloakedBlock; + RevelationAware.register(this); + } + + @Override + public Map getBlockStateCloaks() { + Hashtable hashtable = new Hashtable<>(); + hashtable.put(this.getDefaultState(), cloakedBlock.getDefaultState()); + return hashtable; + } + + @Override + public Pair getItemCloak() { + return null; + } + +} diff --git a/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlockItem.java b/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlockItem.java index cd9e41a..b84a57d 100644 --- a/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlockItem.java +++ b/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedBlockItem.java @@ -7,14 +7,14 @@ import net.minecraft.util.Identifier; import net.minecraft.util.Pair; -import java.util.Hashtable; +import java.util.Map; public class CloakedBlockItem extends BlockItem implements RevelationAware { Identifier cloakAdvancementIdentifier; - Item cloakItem; + BlockItem cloakItem; - public CloakedBlockItem(Block block, Settings settings, Identifier cloakAdvancementIdentifier, Item cloakItem) { + public CloakedBlockItem(Block block, Settings settings, Identifier cloakAdvancementIdentifier, BlockItem cloakItem) { super(block, settings); this.cloakAdvancementIdentifier = cloakAdvancementIdentifier; this.cloakItem = cloakItem; @@ -28,8 +28,8 @@ public Identifier getCloakAdvancementIdentifier() { } @Override - public Hashtable getBlockStateCloaks() { - return new Hashtable<>(); + public Map getBlockStateCloaks() { + return Map.of(this.getBlock().getDefaultState(), this.cloakItem.getBlock().getDefaultState()); } @Override diff --git a/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedItem.java b/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedItem.java index 17a18dc..f0696b6 100644 --- a/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedItem.java +++ b/src/main/java/de/dafuqs/revelationary/api/revelations/CloakedItem.java @@ -6,6 +6,7 @@ import net.minecraft.util.Pair; import java.util.Hashtable; +import java.util.Map; public class CloakedItem extends Item implements RevelationAware { @@ -26,7 +27,7 @@ public Identifier getCloakAdvancementIdentifier() { } @Override - public Hashtable getBlockStateCloaks() { + public Map getBlockStateCloaks() { return new Hashtable<>(); } @@ -35,14 +36,4 @@ public Pair getItemCloak() { return new Pair<>(this, cloakItem); } - @Override - public void onCloak() { - - } - - @Override - public void onUncloak() { - - } - } diff --git a/src/main/java/de/dafuqs/revelationary/api/revelations/RevelationAware.java b/src/main/java/de/dafuqs/revelationary/api/revelations/RevelationAware.java index b3637d6..227f9ca 100644 --- a/src/main/java/de/dafuqs/revelationary/api/revelations/RevelationAware.java +++ b/src/main/java/de/dafuqs/revelationary/api/revelations/RevelationAware.java @@ -1,28 +1,20 @@ package de.dafuqs.revelationary.api.revelations; -import de.dafuqs.revelationary.api.advancements.AdvancementHelper; import de.dafuqs.revelationary.RevelationRegistry; +import de.dafuqs.revelationary.api.advancements.AdvancementHelper; import net.minecraft.block.BlockState; import net.minecraft.block.EntityShapeContext; import net.minecraft.block.ShapeContext; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.loot.LootTable; -import net.minecraft.loot.LootTables; import net.minecraft.loot.context.LootContext; import net.minecraft.loot.context.LootContextParameters; -import net.minecraft.loot.context.LootContextTypes; -import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; import net.minecraft.util.Pair; import org.jetbrains.annotations.Nullable; -import java.util.Collections; -import java.util.Hashtable; -import java.util.List; -import java.util.Objects; +import java.util.Map; public interface RevelationAware { @@ -32,7 +24,7 @@ static void register(RevelationAware revelationAware) { RevelationRegistry.registerRevelationAware(revelationAware); } - Hashtable getBlockStateCloaks(); + Map getBlockStateCloaks(); @Nullable Pair getItemCloak(); @@ -70,31 +62,4 @@ static PlayerEntity getLootPlayerEntity(LootContext.Builder lootContextBuilder) } } - default List getCloakedDroppedStacks(BlockState state, LootContext.Builder builder) { - PlayerEntity lootPlayerEntity = getLootPlayerEntity(builder); - - Identifier identifier; - BlockState cloakedBlockState = null; - if (lootPlayerEntity == null || !isVisibleTo(lootPlayerEntity)) { - cloakedBlockState = RevelationRegistry.getCloak(state); - if (cloakedBlockState == null) { - identifier = state.getBlock().getLootTableId(); - } else { - identifier = cloakedBlockState.getBlock().getLootTableId(); - } - } else { - identifier = state.getBlock().getLootTableId(); - } - - if (identifier == LootTables.EMPTY) { - return Collections.emptyList(); - } else { - LootContext lootContext; - lootContext = builder.parameter(LootContextParameters.BLOCK_STATE, Objects.requireNonNullElse(cloakedBlockState, state)).build(LootContextTypes.BLOCK); - ServerWorld serverWorld = lootContext.getWorld(); - LootTable lootTable = serverWorld.getServer().getLootManager().getTable(identifier); - return lootTable.generateLoot(lootContext); - } - } - } diff --git a/src/main/java/de/dafuqs/revelationary/mixin/AbstractBlockMixin.java b/src/main/java/de/dafuqs/revelationary/mixin/AbstractBlockMixin.java index 760a290..de0af09 100644 --- a/src/main/java/de/dafuqs/revelationary/mixin/AbstractBlockMixin.java +++ b/src/main/java/de/dafuqs/revelationary/mixin/AbstractBlockMixin.java @@ -1,24 +1,17 @@ package de.dafuqs.revelationary.mixin; -import de.dafuqs.revelationary.api.revelations.RevelationAware; import de.dafuqs.revelationary.RevelationRegistry; +import de.dafuqs.revelationary.api.revelations.RevelationAware; import net.minecraft.block.AbstractBlock; import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; -import net.minecraft.loot.LootTable; -import net.minecraft.loot.LootTables; import net.minecraft.loot.context.LootContext; -import net.minecraft.loot.context.LootContextParameters; -import net.minecraft.loot.context.LootContextTypes; -import net.minecraft.server.world.ServerWorld; -import net.minecraft.util.Identifier; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.util.Collections; import java.util.List; @Mixin(AbstractBlock.class) @@ -26,27 +19,14 @@ public class AbstractBlockMixin { @Inject(method = "getDroppedStacks(Lnet/minecraft/block/BlockState;Lnet/minecraft/loot/context/LootContext$Builder;)Ljava/util/List;", at = @At("HEAD"), cancellable = true) public void revelationary$getDroppedStacks(BlockState state, LootContext.Builder builder, CallbackInfoReturnable> cir) { - BlockState cloak = RevelationRegistry.getCloak(state); - if(cloak != null) { + BlockState cloakState = RevelationRegistry.getCloak(state); + if(cloakState != null) { PlayerEntity lootPlayerEntity = RevelationAware.getLootPlayerEntity(builder); if (!RevelationRegistry.isVisibleTo(state, lootPlayerEntity)) { - cir.setReturnValue(getLootForSwappedState(builder, cloak)); + List drops = cloakState.getBlock().getDroppedStacks(cloakState, builder); + cir.setReturnValue(drops); } } } - private List getLootForSwappedState(LootContext.Builder builder, BlockState cloakedState) { - Identifier lootTableId = cloakedState.getBlock().getLootTableId(); - - if (lootTableId == LootTables.EMPTY) { - return Collections.emptyList(); - } else { - LootContext lootContext; - lootContext = builder.parameter(LootContextParameters.BLOCK_STATE, cloakedState).build(LootContextTypes.BLOCK); - ServerWorld serverWorld = lootContext.getWorld(); - LootTable lootTable = serverWorld.getServer().getLootManager().getTable(lootTableId); - return lootTable.generateLoot(lootContext); - } - } - } diff --git a/src/main/java/de/dafuqs/revelationary/mixin/PlayerAdvancementTrackerMixin.java b/src/main/java/de/dafuqs/revelationary/mixin/PlayerAdvancementTrackerMixin.java index 64ff108..bbd2e6d 100644 --- a/src/main/java/de/dafuqs/revelationary/mixin/PlayerAdvancementTrackerMixin.java +++ b/src/main/java/de/dafuqs/revelationary/mixin/PlayerAdvancementTrackerMixin.java @@ -1,7 +1,7 @@ package de.dafuqs.revelationary.mixin; -import de.dafuqs.revelationary.api.advancements.AdvancementCriteria; import de.dafuqs.revelationary.RevelationRegistry; +import de.dafuqs.revelationary.api.advancements.AdvancementCriteria; import net.minecraft.advancement.Advancement; import net.minecraft.advancement.AdvancementProgress; import net.minecraft.advancement.PlayerAdvancementTracker;