Skip to content

Commit

Permalink
Improved code and client.-server interactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crystal-Spider committed May 23, 2022
1 parent d6742c1 commit 6e45d75
Show file tree
Hide file tree
Showing 17 changed files with 315 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.client.color.block.BlockColor;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -37,10 +38,10 @@ public int getColor(BlockState state, @Nullable BlockAndTintGetter getter, @Null
BlockState referredSlabState = VerticalSlabUtils.getReferredSlabState(getter, pos);
if (referredSlabState != null) {
Item slab = referredSlabState.getBlock().asItem();
if (VerticalSlabUtils.slabMap.containsKey(slab)) {
return event.getBlockColors().getColor(Block.byItem(VerticalSlabUtils.slabMap.get(slab)).defaultBlockState(), getter, pos, tintIndex);
if (MapsManager.slabMap.containsKey(slab)) {
return event.getBlockColors().getColor(Block.byItem(MapsManager.slabMap.get(slab)).defaultBlockState(), getter, pos, tintIndex);
}
return event.getBlockColors().getColor(VerticalSlabUtils.slabStateMap.get(slab), getter, pos, tintIndex);
return event.getBlockColors().getColor(MapsManager.slabStateMap.get(slab), getter, pos, tintIndex);
}
}
return -1;
Expand All @@ -64,8 +65,8 @@ public int getColor(ItemStack itemStack, int tintIndex) {
BlockState referredSlabState = VerticalSlabUtils.getReferredSlabState(itemStack);
if (referredSlabState != null) {
Item slab = referredSlabState.getBlock().asItem();
if (VerticalSlabUtils.slabMap.containsKey(slab)) {
return event.getItemColors().getColor(VerticalSlabUtils.slabMap.get(slab).getDefaultInstance(), tintIndex);
if (MapsManager.slabMap.containsKey(slab)) {
return event.getItemColors().getColor(MapsManager.slabMap.get(slab).getDefaultInstance(), tintIndex);
}
return event.getItemColors().getColor(slab.getDefaultInstance(), tintIndex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package crystalspider.justverticalslabs.handlers;

import crystalspider.justverticalslabs.utils.MapsInstantiator;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.searchtree.MutableSearchTree;
import net.minecraft.client.searchtree.SearchRegistry;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraftforge.client.event.RecipesUpdatedEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand All @@ -16,20 +10,17 @@
*/
public class RecipesUpdateEventHandler {
/**
* Handles the event {@link RecipesUpdatedEvent} to add each existing Vertical Slab to the search tree.
* If maps are not computed yet sets {@link MapsManager#fallbackRecipeManager}, otherwise adds Vertical Slabs to the search tree.
*
* @param event
*/
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onRecipesUpdatedEvent(RecipesUpdatedEvent event) {
if (VerticalSlabUtils.slabStateMap == null) {
MapsInstantiator.instantiateMaps(event.getRecipeManager());
if (MapsManager.slabStateMap == null) {
// If maps were not computed yet here it means there is no dedicated server and it's needed to computed them for the client.
MapsManager.setFallbackRecipeManager(event.getRecipeManager());
} else {
MapsManager.addToSearchTree();
}

MutableSearchTree<ItemStack> creativeSearchTree = Minecraft.getInstance().getSearchTree(SearchRegistry.CREATIVE_NAMES);
for(BlockState referredSlabState : VerticalSlabUtils.slabStateMap.values()) {
creativeSearchTree.add(VerticalSlabUtils.getVerticalSlabItem(referredSlabState, VerticalSlabUtils.isTranslucent(referredSlabState)));
}
creativeSearchTree.refresh();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package crystalspider.justverticalslabs.handlers;

import crystalspider.justverticalslabs.utils.MapsInstantiator;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraftforge.event.server.ServerAboutToStartEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand All @@ -10,12 +10,13 @@
*/
public class ServerAboutToStartEventHandler {
/**
* Handles the event {@link ServerAboutToStartEvent} to load the maps of slabs-blocks.
* Handles the event {@link ServerAboutToStartEvent} to compute the maps.
* Compute maps for the server, either dedicated or multiplayer.
*
* @param event - {@link ServerAboutToStartEvent}.
*/
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onServerAboutToStartEvent(ServerAboutToStartEvent event) {
MapsInstantiator.instantiateMaps(event.getServer().getRecipeManager());
MapsManager.computeMaps(event.getServer().getRecipeManager());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package crystalspider.justverticalslabs.handlers;

import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TagsUpdatedEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

/**
* {@link TagsUpdatedEvent} handler.
* Client only.
*/
@EventBusSubscriber(value = Dist.CLIENT, bus = Bus.FORGE)
public class TagsUpdatedEventHandler {
/**
* If not already computed, computes maps and adds Vertical Slabs to the search tree.
*
* @param event
*/
@SubscribeEvent
public static void onTagsUpdatedEvent(TagsUpdatedEvent event) {
if (MapsManager.slabStateMap == null) {
// If maps were not computed yet here it means there is no dedicated server and it's needed to computed them for the client.
MapsManager.computeMaps();
MapsManager.addToSearchTree();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
Expand All @@ -17,8 +18,8 @@ public CutoutVerticalSlabBlockItem() {

@Override
public void fillItemCategory(CreativeModeTab creativeModeTab, NonNullList<ItemStack> itemStacks) {
if (this.allowdedIn(creativeModeTab) && VerticalSlabUtils.slabStateMap != null) {
for(BlockState referredSlabState : VerticalSlabUtils.slabStateMap.values()) {
if (this.allowdedIn(creativeModeTab) && MapsManager.slabStateMap != null) {
for(BlockState referredSlabState : MapsManager.slabStateMap.values()) {
if (!VerticalSlabUtils.isTranslucent(referredSlabState)) {
itemStacks.add(VerticalSlabUtils.getVerticalSlabItem(referredSlabState, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
Expand All @@ -17,8 +18,8 @@ public TranslucentVerticalSlabBlockItem() {

@Override
public void fillItemCategory(CreativeModeTab creativeModeTab, NonNullList<ItemStack> itemStacks) {
if (this.allowdedIn(creativeModeTab) && VerticalSlabUtils.translucentMap != null) {
for(BlockState referredSlabState : VerticalSlabUtils.translucentMap.values()) {
if (this.allowdedIn(creativeModeTab) && MapsManager.translucentMap != null) {
for(BlockState referredSlabState : MapsManager.translucentMap.values()) {
itemStacks.add(VerticalSlabUtils.getVerticalSlabItem(referredSlabState, true));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -52,7 +53,7 @@ public ItemStack getDefaultInstance() {
public int getBurnTime(ItemStack itemStack, @Nullable RecipeType<?> recipeType) {
BlockState referredSlabState = VerticalSlabUtils.getReferredSlabState(itemStack);
if (referredSlabState != null) {
return ForgeHooks.getBurnTime(VerticalSlabUtils.slabStateMap.get(referredSlabState.getBlock().asItem()).getBlock().asItem().getDefaultInstance(), recipeType);
return ForgeHooks.getBurnTime(MapsManager.slabStateMap.get(referredSlabState.getBlock().asItem()).getBlock().asItem().getDefaultInstance(), recipeType);
}
return super.getBurnTime(itemStack, recipeType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import crystalspider.justverticalslabs.blocks.VerticalSlabBlockEntity;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -41,8 +42,8 @@ public List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext contex
LootContext.Builder builder = new LootContext.Builder(context).withOptionalParameter(LootContextParams.BLOCK_ENTITY, null).withParameter(LootContextParams.BLOCK_STATE, referredSlabState);
for (ItemStack slabLoot : referredLootTable.getRandomItems(builder.create(LootContextParamSets.BLOCK))) {
Item itemLoot = slabLoot.getItem();
if (VerticalSlabUtils.slabStateMap.containsKey(itemLoot)) {
loot.add(VerticalSlabUtils.getVerticalSlabItem(VerticalSlabUtils.slabStateMap.get(itemLoot), VerticalSlabUtils.isTranslucent(itemLoot)));
if (MapsManager.slabStateMap.containsKey(itemLoot)) {
loot.add(VerticalSlabUtils.getVerticalSlabItem(MapsManager.slabStateMap.get(itemLoot), VerticalSlabUtils.isTranslucent(itemLoot)));
} else {
loot.add(slabLoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonObject;

import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
Expand Down Expand Up @@ -53,7 +54,7 @@ default boolean isSpecial() {
*/
default boolean isVerticalSlab(ItemStack itemStack) {
BlockState referredSlabState = VerticalSlabUtils.getReferredSlabState(itemStack);
return !itemStack.isEmpty() && referredSlabState != null && VerticalSlabUtils.slabStateMap.containsKey(referredSlabState.getBlock().asItem());
return !itemStack.isEmpty() && referredSlabState != null && MapsManager.slabStateMap.containsKey(referredSlabState.getBlock().asItem());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.recipes.crafting.VerticalSlabCraftingRecipe;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.Item;
Expand All @@ -27,8 +28,8 @@ public BlockToVerticalSlabCraftingRecipe() {

@Override
public ItemStack assemble(ItemStack matchedItem) {
Item slab = VerticalSlabUtils.blockMap.get(matchedItem.getItem());
ItemStack verticalSlab = VerticalSlabUtils.getVerticalSlabItem(VerticalSlabUtils.slabStateMap.get(slab), VerticalSlabUtils.isTranslucent(slab));
Item slab = MapsManager.blockMap.get(matchedItem.getItem());
ItemStack verticalSlab = VerticalSlabUtils.getVerticalSlabItem(MapsManager.slabStateMap.get(slab), VerticalSlabUtils.isTranslucent(slab));
verticalSlab.setCount(6);
return verticalSlab;
}
Expand Down Expand Up @@ -56,7 +57,7 @@ protected Integer getMatchIndex(CraftingContainer craftingContainer) {
ItemStack itemStack1 = craftingContainer.getItem(index);
if (!itemStack1.isEmpty()) {
Item item = itemStack1.getItem();
if (VerticalSlabUtils.blockMap.containsKey(item)) {
if (MapsManager.blockMap.containsKey(item)) {
ItemStack itemStack2 = craftingContainer.getItem(index + containerWidth);
ItemStack itemStack3 = craftingContainer.getItem(index + containerWidth * 2);
if (itemStack2.is(item) && itemStack3.is(item)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.recipes.crafting.VerticalSlabCraftingRecipe;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.Item;
Expand All @@ -27,7 +28,7 @@ public SlabToBlockCraftingRecipe() {

@Override
public ItemStack assemble(ItemStack matchedItem) {
return VerticalSlabUtils.slabMap.get(matchedItem.getItem()).getDefaultInstance();
return MapsManager.slabMap.get(matchedItem.getItem()).getDefaultInstance();
}

/**
Expand All @@ -53,7 +54,7 @@ protected Integer getMatchIndex(CraftingContainer craftingContainer) {
ItemStack itemStack1 = craftingContainer.getItem(index);
if (!itemStack1.isEmpty()) {
Item item = itemStack1.getItem();
if (VerticalSlabUtils.slabMap.containsKey(item)) {
if (MapsManager.slabMap.containsKey(item)) {
ItemStack itemStack2 = craftingContainer.getItem(index + containerWidth);
if (itemStack2.is(item)) {
if (matchIndex == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.recipes.crafting.VerticalSlabCraftingRecipe;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
Expand All @@ -26,7 +27,7 @@ public SlabToVerticalSlabCraftingRecipe() {

@Override
public ItemStack assemble(ItemStack matchedItem) {
return VerticalSlabUtils.getVerticalSlabItem(VerticalSlabUtils.slabStateMap.get(matchedItem.getItem()), VerticalSlabUtils.isTranslucent(matchedItem.getItem()));
return VerticalSlabUtils.getVerticalSlabItem(MapsManager.slabStateMap.get(matchedItem.getItem()), VerticalSlabUtils.isTranslucent(matchedItem.getItem()));
}

/**
Expand All @@ -49,7 +50,7 @@ protected Integer getMatchIndex(CraftingContainer craftingContainer) {
for (int i = 0; i < craftingContainer.getContainerSize() && correctPattern; i++) {
ItemStack itemStack = craftingContainer.getItem(i);
if (!itemStack.isEmpty()) {
if (matchIndex == null && VerticalSlabUtils.slabStateMap.containsKey(itemStack.getItem())) {
if (matchIndex == null && MapsManager.slabStateMap.containsKey(itemStack.getItem())) {
matchIndex = i;
} else {
matchIndex = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.recipes.crafting.VerticalSlabCraftingRecipe;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
Expand All @@ -26,7 +27,7 @@ public VerticalSlabToBlockCraftingRecipe() {

@Override
public ItemStack assemble(ItemStack matchedItem) {
return VerticalSlabUtils.slabMap.get(VerticalSlabUtils.getReferredSlabState(matchedItem).getBlock().asItem()).getDefaultInstance();
return MapsManager.slabMap.get(VerticalSlabUtils.getReferredSlabState(matchedItem).getBlock().asItem()).getDefaultInstance();
}

/**
Expand Down Expand Up @@ -82,7 +83,7 @@ protected Integer getMatchIndex(CraftingContainer craftingContainer) {
* @return whether both {@link ItemStack ItemStacks} represent the same kind of Vertical Slab.
*/
protected boolean verticalSlabsMatch(ItemStack itemStack1, ItemStack itemStack2) {
return VerticalSlabUtils.getReferredSlabState(itemStack1) == VerticalSlabUtils.getReferredSlabState(itemStack2) && VerticalSlabUtils.slabMap.containsKey(VerticalSlabUtils.getReferredSlabState(itemStack1).getBlock().asItem());
return VerticalSlabUtils.getReferredSlabState(itemStack1) == VerticalSlabUtils.getReferredSlabState(itemStack2) && MapsManager.slabMap.containsKey(VerticalSlabUtils.getReferredSlabState(itemStack1).getBlock().asItem());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import crystalspider.justverticalslabs.JustVerticalSlabsLoader;
import crystalspider.justverticalslabs.recipes.crafting.VerticalSlabCraftingRecipe;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils;
import crystalspider.justverticalslabs.utils.VerticalSlabUtils.MapsManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.Item;
Expand All @@ -25,8 +26,8 @@ public WaxedVerticalSlabCraftingRecipe() {

@Override
public ItemStack assemble(ItemStack matchedItem) {
Item slab = VerticalSlabUtils.waxingMap.get(VerticalSlabUtils.getReferredSlabState(matchedItem).getBlock().asItem());
return VerticalSlabUtils.getVerticalSlabItem(VerticalSlabUtils.slabStateMap.get(slab), VerticalSlabUtils.isTranslucent(slab));
Item slab = MapsManager.waxingMap.get(VerticalSlabUtils.getReferredSlabState(matchedItem).getBlock().asItem());
return VerticalSlabUtils.getVerticalSlabItem(MapsManager.slabStateMap.get(slab), VerticalSlabUtils.isTranslucent(slab));
}

/**
Expand Down Expand Up @@ -54,7 +55,7 @@ protected Integer getMatchIndex(CraftingContainer craftingContainer) {
for (int i = 0; i < craftingContainer.getContainerSize() && correctPattern; i++) {
ItemStack itemStack = craftingContainer.getItem(i);
if (!itemStack.isEmpty()) {
if (matchIndex == null && isVerticalSlab(itemStack) && VerticalSlabUtils.waxingMap.containsKey(VerticalSlabUtils.getReferredSlabState(itemStack).getBlock().asItem())) {
if (matchIndex == null && isVerticalSlab(itemStack) && MapsManager.waxingMap.containsKey(VerticalSlabUtils.getReferredSlabState(itemStack).getBlock().asItem())) {
matchIndex = i;
} else if(!hasHoneycomb && itemStack.is(Items.HONEYCOMB)) {
hasHoneycomb = true;
Expand Down
Loading

0 comments on commit 6e45d75

Please sign in to comment.