Skip to content

Commit

Permalink
Update to 1.21(.1)
Browse files Browse the repository at this point in the history
Lotssssssss of changes, seems to be working fine now though.
  • Loading branch information
OffsetMonkey538 committed Dec 15, 2024
1 parent 8e8e5c4 commit 3c075b4
Show file tree
Hide file tree
Showing 28 changed files with 743 additions and 347 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import net.minecraft.client.item.ModelPredicateProviderRegistry;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;
import net.minecraft.client.render.debug.DebugRenderer;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.entity.EntityType;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.math.ColorHelper;
import top.offsetmonkey538.compactmobfarms.block.ModBlocks;
import top.offsetmonkey538.compactmobfarms.block.entity.ModBlockEntityTypes;
import top.offsetmonkey538.compactmobfarms.client.gui.screen.ingame.CompactMobFarmScreen;
Expand Down Expand Up @@ -41,87 +46,79 @@ public void onInitializeClient() {

if (spawnEgg == null) return -1;

return spawnEgg.getColor(tintIndex);
return ColorHelper.Argb.fullAlpha(spawnEgg.getColor(tintIndex));
}, ModItems.FILLED_SAMPLE_TAKER);

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_ENTITY_CHANGED, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;
ClientPlayNetworking.registerGlobalReceiver(ModPackets.GuiEntityChanged.ID, (payload, context) -> {
// IntelliJ thinks I should run '.close()' on the MinecraftClient and close the game as it inherits
// from AutoCloseable. Mr. IntelliJ IDEA is totally right.
// noinspection resource
if (!(context.client().currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != payload.syncId()) return;

EntityType<?> livingEntityType = buf.readRegistryValue(Registries.ENTITY_TYPE);

if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;

screen.setEntity(livingEntityType);
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_ENTITY_REMOVED, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;

screen.clearEntity();
screen.setEntity(payload.newEntity().orElse(null));
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GuiHealthChanged.ID, (payload, context) -> {
// IntelliJ thinks I should run '.close()' on the MinecraftClient and close the game as it inherits
// from AutoCloseable. Mr. IntelliJ IDEA is totally right.
// noinspection resource
if (!(context.client().currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != payload.syncId()) return;

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_HEALTH_RESET, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;

screen.resetEntityHealth();
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_HEALTH_CHANGED, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;

float newHealth = buf.readFloat();

if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;

screen.setEntityHealth(newHealth);
if (payload.newHealth().isEmpty()) screen.resetEntityHealth();
else screen.setEntityHealth(payload.newHealth().get());
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_MAX_HEALTH_CHANGED, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;

float newMaxHealth = buf.readFloat();

if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;
ClientPlayNetworking.registerGlobalReceiver(ModPackets.GuiMaxHealthChanged.ID, (payload, context) -> {
// IntelliJ thinks I should run '.close()' on the MinecraftClient and close the game as it inherits
// from AutoCloseable. Mr. IntelliJ IDEA is totally right.
// noinspection resource
if (!(context.client().currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != payload.syncId()) return;

screen.setMaxEntityHealth(newMaxHealth);
screen.setMaxEntityHealth(payload.newMaxHealth());
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_UPDATE_ATTACK_SPEED, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;

float newAttackSpeed = buf.readFloat();

if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;
ClientPlayNetworking.registerGlobalReceiver(ModPackets.GuiAttackSpeedChanged.ID, (payload, context) -> {
// IntelliJ thinks I should run '.close()' on the MinecraftClient and close the game as it inherits
// from AutoCloseable. Mr. IntelliJ IDEA is totally right.
// noinspection resource
if (!(context.client().currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != payload.syncId()) return;

screen.setAttackSpeed(newAttackSpeed);
screen.setAttackSpeed(payload.newAttackSpeed());
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_UPDATE_ATTACK_DAMAGE, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;
ClientPlayNetworking.registerGlobalReceiver(ModPackets.GuiAttackDamageChanged.ID, (payload, context) -> {
// IntelliJ thinks I should run '.close()' on the MinecraftClient and close the game as it inherits
// from AutoCloseable. Mr. IntelliJ IDEA is totally right.
// noinspection resource
if (!(context.client().currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != payload.syncId()) return;

float newAttackDamage = buf.readFloat();

if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;

screen.setAttackDamage(newAttackDamage);
screen.setAttackDamage(payload.newAttackDamage());
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.GUI_DISPLAY_PROBLEM_MESSAGE, (client, handler, buf, responseSender) -> {
if (!(client.currentScreen instanceof CompactMobFarmScreen screen)) return;

Text problemMessage = buf.readText();
ClientPlayNetworking.registerGlobalReceiver(ModPackets.GuiDisplayProblemMessage.ID, (payload, context) -> {
// IntelliJ thinks I should run '.close()' on the MinecraftClient and close the game as it inherits
// from AutoCloseable. Mr. IntelliJ IDEA is totally right.
// noinspection resource
if (!(context.client().currentScreen instanceof CompactMobFarmScreen screen)) return;
if (screen.getScreenHandler().syncId != payload.syncId()) return;

if (screen.getScreenHandler().syncId != buf.readUnsignedByte()) return;

screen.displayProblemMessage(problemMessage);
screen.displayProblemMessage(payload.problemMessage());
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.UPDATE_ENTITY_TIER_LIST, (client, handler, buf, responseSender) -> {
ClientPlayNetworking.registerGlobalReceiver(ModPackets.EntityTierListChanged.ID, (payload, context) -> {
EntityTiers.INSTANCE.clear();
EntityTiers.INSTANCE.fromUpdatePacket(buf);

EntityTiers.INSTANCE.UNSUPPORTED = payload.unsupported();
EntityTiers.INSTANCE.TIER_0 = payload.tier0();
EntityTiers.INSTANCE.TIER_1 = payload.tier1();
EntityTiers.INSTANCE.TIER_2 = payload.tier2();
EntityTiers.INSTANCE.TIER_3 = payload.tier3();
EntityTiers.INSTANCE.TIER_4 = payload.tier4();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public void register(EmiRegistry registry) {
));
}

registry.setDefaultComparison(ModItems.FILLED_SAMPLE_TAKER, Comparison.compareNbt());
registry.setDefaultComparison(ModItems.FILLED_SAMPLE_TAKER, Comparison.compareComponents());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void renderEntity(DrawContext context, LivingEntity entity, int x,

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);
renderBackground(context, mouseX, mouseY, delta);

super.render(context, mouseX, mouseY, delta);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
Expand All @@ -15,19 +15,22 @@
import net.minecraft.loot.function.SetCountLootFunction;
import net.minecraft.loot.provider.number.UniformLootNumberProvider;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.offsetmonkey538.compactmobfarms.block.ModBlocks;
import top.offsetmonkey538.compactmobfarms.block.entity.ModBlockEntityTypes;
import top.offsetmonkey538.compactmobfarms.component.ModComponents;
import top.offsetmonkey538.compactmobfarms.config.EntityTierResourceReloadListener;
import top.offsetmonkey538.compactmobfarms.config.EntityTiers;
import top.offsetmonkey538.compactmobfarms.item.ModItems;
import top.offsetmonkey538.compactmobfarms.item.group.ModItemGroups;
import top.offsetmonkey538.compactmobfarms.network.ModPackets;
import top.offsetmonkey538.compactmobfarms.recipe.ModRecipes;
import top.offsetmonkey538.compactmobfarms.screen.ModScreenHandlers;
import top.offsetmonkey538.monkeylib538.utils.EnchantmentUtils;
import top.offsetmonkey538.monkeylib538.utils.IdentifierUtils;

public class CompactMobFarms implements ModInitializer {
Expand All @@ -37,34 +40,33 @@ public class CompactMobFarms implements ModInitializer {
@Override
public void onInitialize() {
ModBlocks.register();
ModComponents.register();
ModItems.register();
ModItemGroups.register();
ModBlockEntityTypes.register();
ModScreenHandlers.register();
ModRecipes.register();
ModPackets.register();

//noinspection UnstableApiUsage
ItemStorage.SIDED.registerForBlockEntity((block, direction) -> block.getDropInventory(), ModBlockEntityTypes.COMPACT_MOB_FARM);
ItemStorage.SIDED.registerForBlockEntity((block, direction) -> block.getDropInventory(), ModBlockEntityTypes.COMPACT_MOB_FARM);

LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
if (!Blocks.SPAWNER.getLootTableId().equals(id) || !source.isBuiltin()) return;
LootTableEvents.MODIFY.register((key, tableBuilder, source, registries) -> {
if (Blocks.SPAWNER.getLootTableKey() != key || !source.isBuiltin()) return;

LootPool.Builder pool = LootPool.builder()
.with(ItemEntry.builder(ModItems.SPAWNER_SHARD))
.apply(SetCountLootFunction.builder(UniformLootNumberProvider.create(6, 8)))
.apply(ApplyBonusLootFunction.uniformBonusCount(Enchantments.FORTUNE, 5));
.apply(ApplyBonusLootFunction.uniformBonusCount(registries.createRegistryLookup().getOrThrow(RegistryKeys.ENCHANTMENT).getOrThrow(Enchantments.FORTUNE), 5));

tableBuilder.pool(pool);
});

ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new EntityTierResourceReloadListener());

ServerLifecycleEvents.SYNC_DATA_PACK_CONTENTS.register((player, joined) -> {
final PacketByteBuf buf = PacketByteBufs.create();

EntityTiers.INSTANCE.toUpdatePacket(buf);

ServerPlayNetworking.send(player, ModPackets.UPDATE_ENTITY_TIER_LIST, buf);
ServerPlayNetworking.send(player, new ModPackets.EntityTierListChanged(
EntityTiers.INSTANCE
));
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package top.offsetmonkey538.compactmobfarms.block;

import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.ShapeContext;

import com.mojang.serialization.MapCodec;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
Expand All @@ -37,9 +33,7 @@
import top.offsetmonkey538.compactmobfarms.block.entity.ModBlockEntityTypes;
import top.offsetmonkey538.compactmobfarms.item.ModItems;

import static top.offsetmonkey538.compactmobfarms.block.entity.CompactMobFarmBlockEntity.*;

public class CompactMobFarmBlock extends BlockWithEntity {
public class CompactMobFarmBlock extends Block implements BlockEntityProvider {
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
public static final VoxelShape SHAPE = VoxelShapes.union(
VoxelShapes.cuboid(0.05625f, 0.0625f, 0.05625f, 0.94375f, 0.9375f, 0.94375f),
Expand All @@ -56,8 +50,12 @@ public CompactMobFarmBlock(Settings settings) {
}

@Override
@SuppressWarnings("deprecation")
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
protected MapCodec<? extends BlockWithEntity> getCodec() {
return null;
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (world.isClient()) return ActionResult.SUCCESS;

NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
Expand All @@ -68,13 +66,15 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
return ActionResult.SUCCESS;
}

@Nullable
@Override
public void appendTooltip(ItemStack stack, @Nullable BlockView world, List<Text> tooltip, TooltipContext options) {
final NbtCompound nbt = BlockItem.getBlockEntityNbt(stack);
if (nbt == null) return;
protected NamedScreenHandlerFactory createScreenHandlerFactory(BlockState state, World world, BlockPos pos) {
return (world.getBlockEntity(pos) instanceof NamedScreenHandlerFactory factory) ? factory : null;
}

ItemStack sampleTakerStack = ItemStack.fromNbt(nbt.getList(SAMPLE_TAKER_NBT_KEY, NbtElement.COMPOUND_TYPE).getCompound(0));
ModItems.FILLED_SAMPLE_TAKER.appendTooltip(sampleTakerStack, null, tooltip, options);
@Override
public void appendTooltip(ItemStack stack, Item.TooltipContext context, List<Text> tooltip, TooltipType options) {
ModItems.FILLED_SAMPLE_TAKER.appendTooltip(stack, null, tooltip, options);
}

@Nullable
Expand All @@ -89,24 +89,22 @@ public BlockRenderType getRenderType(BlockState state) {
}

@Override
@SuppressWarnings("deprecation")
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return SHAPE;
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return checkType(type, ModBlockEntityTypes.COMPACT_MOB_FARM, (world1, pos, state1, blockEntity) -> CompactMobFarmBlockEntity.tick(world1, blockEntity));
if (type != ModBlockEntityTypes.COMPACT_MOB_FARM) return null;
return (world1, pos, state1, blockEntity) -> CompactMobFarmBlockEntity.tick(world1, (CompactMobFarmBlockEntity) blockEntity);
}

@SuppressWarnings("deprecation")
@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return state.with(FACING, rotation.rotate(state.get(FACING)));
}

@SuppressWarnings("deprecation")
@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return state.rotate(mirror.getRotation(state.get(FACING)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package top.offsetmonkey538.compactmobfarms.block;

import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.MapColor;
import net.minecraft.block.enums.Instrument;
import net.minecraft.block.enums.NoteBlockInstrument;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.sound.BlockSoundGroup;
Expand All @@ -15,7 +15,7 @@ private ModBlocks() {

}

public static final Block COMPACT_MOB_FARM = register(new CompactMobFarmBlock(FabricBlockSettings.create().mapColor(MapColor.IRON_GRAY).instrument(Instrument.IRON_XYLOPHONE).sounds(BlockSoundGroup.METAL).strength(50.0f, 1200.0f).nonOpaque().notSolid().requiresTool()), "compact_mob_farm");
public static final Block COMPACT_MOB_FARM = register(new CompactMobFarmBlock(AbstractBlock.Settings.create().mapColor(MapColor.IRON_GRAY).instrument(NoteBlockInstrument.IRON_XYLOPHONE).sounds(BlockSoundGroup.METAL).strength(50.0f, 1200.0f).nonOpaque().requiresTool()), "compact_mob_farm");

@SuppressWarnings("SameParameterValue")
private static <T extends Block> T register(T block, String name) {
Expand Down
Loading

0 comments on commit 3c075b4

Please sign in to comment.