Skip to content

Commit

Permalink
lots of code optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
DaFuqs committed Jun 14, 2022
1 parent d9b06b1 commit 63055c9
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ClientAdvancements {

protected static boolean receivedFirstAdvancementPacket = false;
protected static List<ClientAdvancementPacketCallback> callbacks = new ArrayList<>();
public static List<ClientAdvancementPacketCallback> callbacks = new ArrayList<>();

public static void onClientPacket(@NotNull AdvancementUpdateS2CPacket packet) {
boolean hadPacketBefore = receivedFirstAdvancementPacket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BlockState, BlockState> getBlockStateCloaks() {
Hashtable<BlockState, BlockState> hashtable = new Hashtable<>();
hashtable.put(this.getDefaultState(), cloakedBlock.getDefaultState());
return hashtable;
}

@Override
public Pair<Item, Item> getItemCloak() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,8 +28,8 @@ public Identifier getCloakAdvancementIdentifier() {
}

@Override
public Hashtable<BlockState, BlockState> getBlockStateCloaks() {
return new Hashtable<>();
public Map<BlockState, BlockState> getBlockStateCloaks() {
return Map.of(this.getBlock().getDefaultState(), this.cloakItem.getBlock().getDefaultState());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.util.Pair;

import java.util.Hashtable;
import java.util.Map;

public class CloakedItem extends Item implements RevelationAware {

Expand All @@ -26,7 +27,7 @@ public Identifier getCloakAdvancementIdentifier() {
}

@Override
public Hashtable<BlockState, BlockState> getBlockStateCloaks() {
public Map<BlockState, BlockState> getBlockStateCloaks() {
return new Hashtable<>();
}

Expand All @@ -35,14 +36,4 @@ public Pair<Item, Item> getItemCloak() {
return new Pair<>(this, cloakItem);
}

@Override
public void onCloak() {

}

@Override
public void onUncloak() {

}

}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -32,7 +24,7 @@ static void register(RevelationAware revelationAware) {
RevelationRegistry.registerRevelationAware(revelationAware);
}

Hashtable<BlockState, BlockState> getBlockStateCloaks();
Map<BlockState, BlockState> getBlockStateCloaks();

@Nullable Pair<Item, Item> getItemCloak();

Expand Down Expand Up @@ -70,31 +62,4 @@ static PlayerEntity getLootPlayerEntity(LootContext.Builder lootContextBuilder)
}
}

default List<ItemStack> 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);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,52 +1,32 @@
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)
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<List<ItemStack>> 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<ItemStack> drops = cloakState.getBlock().getDroppedStacks(cloakState, builder);
cir.setReturnValue(drops);
}
}
}

private List<ItemStack> 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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 63055c9

Please sign in to comment.