Skip to content

Commit

Permalink
wip: Continue port to Minecraft 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Nov 1, 2024
1 parent 503ad7d commit 811736a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
Expand Down Expand Up @@ -129,7 +130,7 @@ public AutoCompressorBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockSta
}

public boolean shouldCompress(Multiset<CompressedRecipe> inputItems, CompressedRecipe compressedRecipe) {
return inputItems.count(compressedRecipe) >= compressedRecipe.getCount();
return inputItems.count(compressedRecipe) >= compressedRecipe.count();
}

public static void serverTick(Level level, BlockPos pos, BlockState state, AutoCompressorBlockEntity blockEntity) {
Expand All @@ -151,24 +152,24 @@ public void serverTick() {
}
}
for (CompressedRecipe compressedRecipe : inputItems.elementSet()) {
Ingredient ingredient = compressedRecipe.getIngredient();
Ingredient ingredient = compressedRecipe.ingredient();
if (shouldCompress(inputItems, compressedRecipe)) {
int space = 0;
for (int i = 0; i < outputSlots.getContainerSize(); i++) {
ItemStack slotStack = outputSlots.getItem(i);
if (slotStack.isEmpty()) {
space = 64;
} else if (isItemEqualWildcard(slotStack, compressedRecipe.getResultStack())) {
} else if (isItemEqualWildcard(slotStack, compressedRecipe.resultStack())) {
space += slotStack.getMaxStackSize() - slotStack.getCount();
}
if (space >= compressedRecipe.getResultStack().getCount()) {
if (space >= compressedRecipe.resultStack().getCount()) {
break;
}
}
if (space < compressedRecipe.getResultStack().getCount()) {
if (space < compressedRecipe.resultStack().getCount()) {
continue;
}
int count = compressedRecipe.getCount();
int count = compressedRecipe.count();
for (int i = 0; i < inputSlots.getContainerSize(); i++) {
ItemStack slotStack = inputSlots.getItem(i);
if (!slotStack.isEmpty() && ingredient.test(slotStack)) {
Expand Down Expand Up @@ -200,7 +201,7 @@ public void serverTick() {
if (!level.isClientSide) {
CompressedRecipe compressedRecipe = currentRecipe;
if (compressedRecipe != null) {
ItemStack resultStack = compressedRecipe.getResultStack().copy();
ItemStack resultStack = compressedRecipe.resultStack().copy();
if (!addItemToOutput(resultStack)) {
overflowBuffer.add(resultStack);
}
Expand Down Expand Up @@ -255,11 +256,8 @@ public float getEffectiveSpeed() {

@Override
public void loadAdditional(CompoundTag tagCompound, HolderLookup.Provider provider) {
if (tagCompound.contains("CurrentRecipeResult")) {
ItemStack itemStack = ItemStack.parseOptional(provider, tagCompound.getCompound("CurrentRecipeResult"));
if (!itemStack.isEmpty()) {
// TODO currentRecipe = new CompressedRecipe(Ingredient.EMPTY, 0, itemStack);
}
if (tagCompound.contains("CurrentRecipe")) {
currentRecipe = ExRegistries.getCompressedRecipeRegistry().getRecipeById(ResourceLocation.parse(tagCompound.getString("CurrentRecipe")));
}
isDisabledByRedstone = tagCompound.getBoolean("IsDisabledByRedstone");
progress = tagCompound.getFloat("Progress");
Expand All @@ -274,7 +272,7 @@ public void loadAdditional(CompoundTag tagCompound, HolderLookup.Provider provid
@Override
public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
if (currentRecipe != null) {
tag.put("CurrentRecipeResult", currentRecipe.getResultStack().save(provider));
tag.putString("CurrentRecipe", currentRecipe.id().toString());
}
tag.putBoolean("IsDisabledByRedstone", isDisabledByRedstone);
tag.putFloat("Progress", progress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public RationingAutoCompressorBlockEntity(BlockPos pos, BlockState state) {

@Override
public boolean shouldCompress(Multiset<CompressedRecipe> inputItems, CompressedRecipe compressedRecipe) {
return inputItems.count(compressedRecipe) >= compressedRecipe.getCount() + 1;
return inputItems.count(compressedRecipe) >= compressedRecipe.count() + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.event.DigSpeedEvent;
import net.blay09.mods.excompressum.registry.ExRegistries;
import net.blay09.mods.excompressum.tag.ModBlockTags;
import net.blay09.mods.excompressum.tag.ModItemTags;
import net.blay09.mods.excompressum.utils.StupidUtils;
import net.minecraft.core.component.DataComponents;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;

public class HammerSpeedHandler {

Expand All @@ -17,15 +14,8 @@ public static void initialize() {
}

public static void onDigSpeed(DigSpeedEvent event) {
ItemStack heldItem = event.getPlayer().getItemInHand(InteractionHand.MAIN_HAND);
final var targetItem = StupidUtils.getItemStackFromState(event.getState());
final var level = event.getPlayer().level();
if (!(level instanceof ServerLevel serverLevel)) {
return; // TODO I believe this needs to run on client too though... might have to upgrade more stuff to tags and to be less dependent on recipe existence
}

if ((heldItem.is(ModItemTags.HAMMERS) || heldItem.is(ModItemTags.COMPRESSED_HAMMERS)) && (ExRegistries.getHammerRegistry()
.isHammerable(serverLevel, targetItem) || ExRegistries.getCompressedHammerRegistry().isHammerable(serverLevel, targetItem))) {
final var heldItem = event.getPlayer().getItemInHand(InteractionHand.MAIN_HAND);
if ((heldItem.is(ModItemTags.HAMMERS) || heldItem.is(ModItemTags.COMPRESSED_HAMMERS)) && event.getState().is(ModBlockTags.MINEABLE_WITH_HAMMER)) {
float newSpeed = 2f;
final var tool = heldItem.get(DataComponents.TOOL);
if (tool != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public InteractionResult useOn(UseOnContext context) {
if (!inventoryStack.isEmpty()) {
if (ExCompressumAPI.getExNihilo().isCompressableOre(inventoryStack)) {
CompressedRecipe recipe = ExRegistries.getCompressedRecipeRegistry().getRecipe(inventoryStack);
if (recipe != null && recipe.getResultStack().getCount() == 1) {
if (inventoryStack.getCount() >= recipe.getCount()) {
if (recipe != null && recipe.resultStack().getCount() == 1) {
if (inventoryStack.getCount() >= recipe.count()) {
BlockState oldState = level.getBlockState(pos);
ItemStack resultStack = recipe.getResultStack().copy();
ItemStack resultStack = recipe.resultStack().copy();
resultStack.getItem().useOn(new UseOnContext(player, context.getHand(), new BlockHitResult(context.getClickLocation(), context.getClickedFace(), context.getClickedPos(), context.isInside())));
level.sendBlockUpdated(pos, oldState, level.getBlockState(pos), 3);
if (resultStack.isEmpty()) {
inventoryStack.shrink(recipe.getCount());
inventoryStack.shrink(recipe.count());
if (inventoryStack.isEmpty()) {
player.getInventory().items.remove(i);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
package net.blay09.mods.excompressum.registry.compressor;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;

public class CompressedRecipe {
private final Ingredient ingredient;
private final int count;
private final ItemStack resultStack;

public CompressedRecipe(Ingredient ingredient, int count, ItemStack resultStack) {
this.ingredient = ingredient;
this.count = count;
this.resultStack = resultStack;
}

public Ingredient getIngredient() {
return ingredient;
}

public int getCount() {
return count;
}

public ItemStack getResultStack() {
return resultStack;
}
public record CompressedRecipe(ResourceLocation id, Ingredient ingredient, int count, ItemStack resultStack) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@

public class CompressedRecipeRegistry {

private final Map<ResourceLocation, CompressedRecipe> recipesById = new HashMap<>();
private final List<CompressedRecipe> recipesSmall = new ArrayList<>();
private final List<CompressedRecipe> recipes = new ArrayList<>();

private final Map<ResourceLocation, CompressedRecipe> cachedResults = new HashMap<>();
private final Map<ResourceLocation, CompressedRecipe> cacheByItemId = new HashMap<>();

public CompressedRecipeRegistry() {
}

public void reloadRecipes(RecipeManager recipeManager, RegistryAccess registryAccess) {
cachedResults.clear();
recipesById.clear();
cacheByItemId.clear();
recipesSmall.clear();
recipes.clear();

Expand Down Expand Up @@ -66,11 +68,11 @@ public void reloadRecipes(RecipeManager recipeManager, RegistryAccess registryAc
final var result = ((ShapedRecipeAccessor) shapedRecipe).getResult();
if (count == 4 && shapedRecipe.getWidth() == 2 && shapedRecipe.getHeight() == 2) {
if (passes) {
recipesSmall.add(new CompressedRecipe(first, 4, result.copy()));
recipesSmall.add(new CompressedRecipe(recipeHolder.id().location(), first, 4, result.copy()));
}
} else if (count == 9 && shapedRecipe.getWidth() == 3 && shapedRecipe.getHeight() == 3) {
if (passes) {
recipes.add(new CompressedRecipe(first, 9, result.copy()));
recipes.add(new CompressedRecipe(recipeHolder.id().location(), first, 9, result.copy()));
}
}
}
Expand All @@ -97,11 +99,11 @@ public void reloadRecipes(RecipeManager recipeManager, RegistryAccess registryAc
final var result = ((ShapelessRecipeAccessor) shapelessRecipe).getResult();
if (count == 4) {
if (passes) {
recipesSmall.add(new CompressedRecipe(first, 4, result.copy()));
recipesSmall.add(new CompressedRecipe(recipeHolder.id().location(), first, 4, result.copy()));
}
} else {
if (passes) {
recipes.add(new CompressedRecipe(first, 9, result.copy()));
recipes.add(new CompressedRecipe(recipeHolder.id().location(), first, 9, result.copy()));
}
}
}
Expand All @@ -116,27 +118,30 @@ public CompressedRecipe getRecipe(ItemStack itemStack) {
}

final ResourceLocation registryName = Balm.getRegistries().getKey(itemStack.getItem());
CompressedRecipe foundRecipe = cachedResults.get(registryName);
CompressedRecipe foundRecipe = cacheByItemId.get(registryName);
if (foundRecipe != null) {
return foundRecipe;
}

for (CompressedRecipe recipe : recipes) {
if (recipe.getIngredient().test(itemStack)) {
cachedResults.put(registryName, recipe);
if (recipe.ingredient().test(itemStack)) {
cacheByItemId.put(registryName, recipe);
return recipe;
}
}

for (CompressedRecipe recipe : recipesSmall) {
if (recipe.getIngredient().test(itemStack)) {
cachedResults.put(registryName, recipe);
if (recipe.ingredient().test(itemStack)) {
cacheByItemId.put(registryName, recipe);
return recipe;
}
}

cachedResults.put(registryName, null);
cacheByItemId.put(registryName, null);
return null;
}

public CompressedRecipe getRecipeById(ResourceLocation id) {
return recipesById.get(id);
}
}

0 comments on commit 811736a

Please sign in to comment.