Compare commits

..

3 commits

Author SHA1 Message Date
Flanks255
08112ea11c Derp derp 2021-10-16 12:09:59 -05:00
Flanks255
cd37abb4ca Network done for now? 2021-10-16 11:46:41 -05:00
Flanks255
3da868fc29 Some low hanging network fruit. 2021-10-16 08:35:52 -05:00
10 changed files with 80 additions and 142 deletions

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.items.base;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockPlant;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemSeeds;

View file

@ -13,15 +13,11 @@ package de.ellpeck.actuallyadditions.mod.misc;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.BannerPattern;
import net.minecraftforge.common.util.EnumHelper;
import java.util.Locale;
public final class BannerHelper {
public static void init() {
addCraftingPattern("drill", new ItemStack(ActuallyItems.DRILL.get(), 1, 3));
addCraftingPattern("drill", new ItemStack(ActuallyItems.DRILL.get()));
addCraftingPattern("leaf_blo", new ItemStack(ActuallyItems.LEAF_BLOWER.get()));
addCraftingPattern("phan_con", new ItemStack(ActuallyItems.PHANTOM_CONNECTOR.get()));
addCraftingPattern("book", new ItemStack(ActuallyItems.ITEM_BOOKLET.get()));
@ -47,7 +43,7 @@ public final class BannerHelper {
public static void addCraftingPattern(String name, ItemStack craftingStack) {
Class<?>[] paramTypes = {String.class, String.class, ItemStack.class};
Object[] paramValues = {ActuallyAdditions.MODID + "_" + name, ActuallyAdditions.MODID + "_" + name, craftingStack};
EnumHelper.addEnum(BannerPattern.class, (ActuallyAdditions.MODID + "_" + name).toUpperCase(Locale.ROOT), paramTypes, paramValues);
// EnumHelper.addEnum(BannerPattern.class, (ActuallyAdditions.MODID + "_" + name).toUpperCase(Locale.ROOT), paramTypes, paramValues); //TODO wth banners
}
}

View file

@ -35,6 +35,7 @@ import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.CompoundNBT;
@ -113,7 +114,7 @@ public class MethodHandler implements IMethodHandler {
int prevCounter = tag.putInt("Counter");
CompoundNBT compound = new CompoundNBT();
compound.putInt("ID", Potion.getIdFromPotion(effect.getEffect()));
//compound.putInt("ID", Potion.getIdFromPotion(effect.getEffect())); //TODO ?!
compound.putInt("Duration", effect.getDuration());
compound.putInt("Amplifier", effect.getAmplifier());
@ -178,7 +179,7 @@ public class MethodHandler implements IMethodHandler {
if (StackUtil.isValid(output)) {
tile.getWorldObject().levelEvent(2001, pos, Block.getId(state));
recipe.transformHook(ItemStack.EMPTY, state, pos, tile);
if (output.getItem() instanceof ItemBlock) {
if (output.getItem() instanceof BlockItem) {
Block toPlace = Block.byItem(output.getItem());
BlockState state2Place = toPlace.getStateForPlacement(tile.getWorldObject(), pos, facing, 0, 0, 0, output.getMetadata(), FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject()), Hand.MAIN_HAND);
tile.getWorldObject().setBlock(pos, state2Place, 2);

View file

@ -16,6 +16,8 @@ import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier;
public class PacketClientToServer {
private CompoundNBT data;
@ -30,39 +32,31 @@ public class PacketClientToServer {
this.handler = handler;
}
@Override
public void fromBytes(ByteBuf buf) {
PacketBuffer buffer = new PacketBuffer(buf);
public static PacketClientToServer fromBytes(PacketBuffer buffer) {
try {
this.data = buffer.readNbt();
CompoundNBT data = buffer.readNbt();
int handlerId = buffer.readInt();
if (handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()) {
this.handler = PacketHandler.DATA_HANDLERS.get(handlerId);
return new PacketClientToServer(data, PacketHandler.DATA_HANDLERS.get(handlerId));
}
} catch (Exception e) {
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a server packet!", e);
}
return new PacketClientToServer();
}
@Override
public void toBytes(ByteBuf buf) {
PacketBuffer buffer = new PacketBuffer(buf);
buffer.writeNbt(this.data);
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(this.handler));
public static void toBytes(PacketClientToServer message, PacketBuffer buffer) {
buffer.writeNbt(message.data);
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(message.handler));
}
public static class Handler implements IMessageHandler<PacketClientToServer, IMessage> {
@Override
public IMessage onMessage(PacketClientToServer message, NetworkEvent.Context ctx) {
FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> {
public static void handle(final PacketClientToServer message, final Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork( () -> {
if (message.data != null && message.handler != null) {
message.handler.handleData(message.data, ctx);
message.handler.handleData(message.data, ctx.get());
}
});
return null;
}
ctx.get().setPacketHandled(true);
}
}

View file

@ -27,14 +27,18 @@ import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;
@ -48,19 +52,19 @@ public final class PacketHandler {
public static final IDataHandler LASER_HANDLER = new IDataHandler() {
@Override
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
AssetUtil.spawnLaserWithTimeClient(compound.getDouble("StartX"), compound.getDouble("StartY"), compound.getDouble("StartZ"), compound.getDouble("EndX"), compound.getDouble("EndY"), compound.getDouble("EndZ"), new float[]{compound.getFloat("Color1"), compound.getFloat("Color2"), compound.getFloat("Color3")}, compound.getInt("MaxAge"), compound.getDouble("RotationTime"), compound.getFloat("Size"), compound.getFloat("Alpha"));
}
};
public static final IDataHandler TILE_ENTITY_HANDLER = new IDataHandler() {
@Override
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
World world = Minecraft.getInstance().level;
if (world != null) {
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
if (tile instanceof TileEntityBase) {
((TileEntityBase) tile).readSyncableNBT(compound.getCompoundTag("Data"), TileEntityBase.NBTType.SYNC);
((TileEntityBase) tile).readSyncableNBT(compound.getCompound("Data"), TileEntityBase.NBTType.SYNC);
}
}
}
@ -68,9 +72,9 @@ public final class PacketHandler {
public static final IDataHandler LASER_PARTICLE_HANDLER = new IDataHandler() {
@Override
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
Minecraft mc = Minecraft.getInstance();
ItemStack stack = new ItemStack(compound);
ItemStack stack = ItemStack.of(compound);
double inX = compound.getDouble("InX") + 0.5;
double inY = compound.getDouble("InY") + 0.78;
@ -81,11 +85,11 @@ public final class PacketHandler {
double outZ = compound.getDouble("OutZ") + 0.5;
Particle fx = new ParticleLaserItem(mc.level, outX, outY, outZ, stack, 0.025, inX, inY, inZ);
mc.effectRenderer.addEffect(fx);
//mc.effectRenderer.addEffect(fx); //TODO
}
};
public static final IDataHandler GUI_BUTTON_TO_TILE_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
if (tile instanceof IButtonReactor) {
@ -97,7 +101,7 @@ public final class PacketHandler {
}
};
public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
Entity entity = world.getEntity(compound.getInt("PlayerID"));
if (entity instanceof PlayerEntity) {
Container container = ((PlayerEntity) entity).containerMenu;
@ -107,7 +111,7 @@ public final class PacketHandler {
}
};
public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
if (tile instanceof INumberReactor) {
@ -116,7 +120,7 @@ public final class PacketHandler {
}
};
public static final IDataHandler GUI_STRING_TO_TILE_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
if (tile instanceof IStringReactor) {
@ -127,9 +131,9 @@ public final class PacketHandler {
public static final IDataHandler SYNC_PLAYER_DATA = new IDataHandler() {
@Override
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
CompoundNBT dataTag = compound.getCompoundTag("Data");
PlayerEntity player = ActuallyAdditions.PROXY.getCurrentPlayer();
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
CompoundNBT dataTag = compound.getCompound("Data");
PlayerEntity player = context.getSender(); //ActuallyAdditions.PROXY.getCurrentPlayer();
if (player != null) {
PlayerData.getDataFromPlayer(player).readFromNBT(dataTag, false);
@ -143,8 +147,8 @@ public final class PacketHandler {
}
};
public static final IDataHandler PLAYER_DATA_TO_SERVER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInt("World"));
PlayerEntity player = world.getPlayerEntityByUUID(compound.getUUID("UUID"));
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("World"))));
PlayerEntity player = world.getServer().getPlayerList().getPlayer(compound.getUUID("UUID"));
if (player != null) {
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
@ -179,8 +183,16 @@ public final class PacketHandler {
);
public static void init() {
THE_NETWORK.registerMessage(PacketServerToClient.Handler.class, PacketServerToClient.class, 0, Side.CLIENT);
THE_NETWORK.registerMessage(PacketClientToServer.Handler.class, PacketClientToServer.class, 1, Side.SERVER);
THE_NETWORK.messageBuilder(PacketServerToClient.class, 0, NetworkDirection.PLAY_TO_CLIENT)
.decoder(PacketServerToClient::fromBytes)
.encoder(PacketServerToClient::toBytes)
.consumer(PacketServerToClient::handle).add();
THE_NETWORK.messageBuilder(PacketClientToServer.class, 1, NetworkDirection.PLAY_TO_SERVER)
.decoder(PacketClientToServer::fromBytes)
.encoder(PacketClientToServer::toBytes)
.consumer(PacketClientToServer::handle).add();
DATA_HANDLERS.add(LASER_HANDLER);
DATA_HANDLERS.add(TILE_ENTITY_HANDLER);

View file

@ -23,6 +23,7 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.PacketDistributor;
public final class PacketHandlerHelper {
@ -33,7 +34,7 @@ public final class PacketHandlerHelper {
compound.putInt("X", pos.getX());
compound.putInt("Y", pos.getY());
compound.putInt("Z", pos.getZ());
compound.putInt("WorldID", tile.getLevel().getDimension());
compound.putString("WorldID", tile.getLevel().dimension().getRegistryName().toString());
compound.putInt("PlayerID", Minecraft.getInstance().player.getId());
compound.putInt("ButtonID", buttonId);
PacketHandler.THE_NETWORK.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
@ -48,7 +49,7 @@ public final class PacketHandlerHelper {
compound.put("Data", data);
if (player instanceof ServerPlayerEntity) {
PacketHandler.THE_NETWORK.sendTo(new PacketServerToClient(compound, PacketHandler.SYNC_PLAYER_DATA), (ServerPlayerEntity) player);
PacketHandler.THE_NETWORK.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), new PacketServerToClient(compound, PacketHandler.SYNC_PLAYER_DATA));
}
}
@ -60,8 +61,8 @@ public final class PacketHandlerHelper {
PlayerEntity player = Minecraft.getInstance().player;
if (player != null) {
compound.putInt("World", player.level.provider.getDimension());
compound.setUniqueId("UUID", player.getUUID());
compound.putString("World", player.level.dimension().getRegistryName().toString());
compound.putUUID("UUID", player.getUUID());
PlayerSave data = PlayerData.getDataFromPlayer(player);
@ -94,7 +95,7 @@ public final class PacketHandlerHelper {
compound.putInt("X", tile.getBlockPos().getX());
compound.putInt("Y", tile.getBlockPos().getY());
compound.putInt("Z", tile.getBlockPos().getZ());
compound.putInt("WorldID", tile.getLevel().provider.getDimension());
compound.putString("WorldID", tile.getLevel().dimension().getRegistryName().toString());
compound.putInt("PlayerID", Minecraft.getInstance().player.getId());
compound.putInt("NumberID", id);
compound.putDouble("Number", number);

View file

@ -11,18 +11,14 @@
package de.ellpeck.actuallyadditions.mod.network;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier;
public class PacketServerToClient implements Message {
public class PacketServerToClient {
private CompoundNBT data;
private IDataHandler handler;
@ -36,40 +32,33 @@ public class PacketServerToClient implements Message {
this.handler = handler;
}
@Override
public void fromBytes(ByteBuf buf) {
PacketBuffer buffer = new PacketBuffer(buf);
public static PacketServerToClient fromBytes(final PacketBuffer buffer) {
try {
this.data = buffer.readNbt();
CompoundNBT data = buffer.readNbt();
int handlerId = buffer.readInt();
if (handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()) {
this.handler = PacketHandler.DATA_HANDLERS.get(handlerId);
return new PacketServerToClient(data, PacketHandler.DATA_HANDLERS.get(handlerId));
}
} catch (Exception e) {
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a client packet!", e);
}
return new PacketServerToClient();
}
@Override
public void toBytes(ByteBuf buf) {
PacketBuffer buffer = new PacketBuffer(buf);
buffer.writeNbt(this.data);
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(this.handler));
public static void toBytes(final PacketServerToClient message, PacketBuffer buffer) {
buffer.writeNbt(message.data);
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(message.handler));
}
public static class Handler implements IMessageHandler<PacketServerToClient, IMessage> {
@Override
@OnlyIn(Dist.CLIENT)
public IMessage onMessage(PacketServerToClient message, MessageContext ctx) {
Minecraft.getInstance().addScheduledTask(() -> {
public static void handle(final PacketServerToClient message, final Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(
() -> {
if (message.data != null && message.handler != null) {
message.handler.handleData(message.data, ctx);
message.handler.handleData(message.data, ctx.get());
}
});
return null;
}
);
ctx.get().setPacketHandled(true);
}
}

View file

@ -10,6 +10,9 @@
package de.ellpeck.actuallyadditions.mod.tile;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public interface IEnergyDisplay {
@OnlyIn(Dist.CLIENT)

View file

@ -11,7 +11,6 @@
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.recipe.EmpowererRecipe;
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.crafting.ActuallyRecipes;
import de.ellpeck.actuallyadditions.mod.crafting.EmpowererRecipe;
@ -30,7 +29,6 @@ import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase.NBTType;
import net.minecraftforge.fml.server.ServerLifecycleHooks;
public class TileEntityEmpowerer extends TileEntityInventoryBase {
@ -43,23 +41,8 @@ public class TileEntityEmpowerer extends TileEntityInventoryBase {
super(ActuallyBlocks.EMPOWERER.getTileEntityType(), 1);
}
@Deprecated //Use findMatchingRecipe
public static List<EmpowererRecipe> getRecipesForInput(ItemStack input) {
List<EmpowererRecipe> recipesThatWork = new ArrayList<>();
if (StackUtil.isValid(input)) {
// TODO: [port] VALIDATOR OR REMOVE
for (EmpowererRecipe recipe : ActuallyAdditionsAPI.EMPOWERER_RECIPES) {
if (recipe.getInput().test(input)) {
recipesThatWork.add(recipe);
}
}
}
return recipesThatWork;
}
public static boolean isPossibleInput(ItemStack stack) {
for (EmpowererRecipe r : ActuallyAdditionsAPI.EMPOWERER_RECIPES) {
// TODO: [port] move to proper recipe system
for (EmpowererRecipe r : ServerLifecycleHooks.getCurrentServer().getRecipeManager().getAllRecipesFor(ActuallyRecipes.Types.EMPOWERING)) {
if (r.getInput().test(stack)) {
return true;
}
@ -86,7 +69,7 @@ public class TileEntityEmpowerer extends TileEntityInventoryBase {
if (stands != null) {
EmpowererRecipe recipe = findMatchingRecipe(this.inv.getStackInSlot(0), stands[0].getStack(), stands[1].getStack(), stands[2].getStack(), stands[3].getStack());
if (recipe != null) {
this.recipeForRenderIndex = ActuallyAdditionsAPI.EMPOWERER_RECIPES.indexOf(recipe);
//this.recipeForRenderIndex = ActuallyAdditionsAPI.EMPOWERER_RECIPES.indexOf(recipe); //TODO whats this?
boolean hasPower = true;

View file

@ -1,42 +0,0 @@
/*
* This file ("RecipeUtil.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.util;
import java.util.List;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.recipe.CrusherRecipe;
import de.ellpeck.actuallyadditions.api.recipe.EmpowererRecipe;
import de.ellpeck.actuallyadditions.api.recipe.LensConversionRecipe;
import de.ellpeck.actuallyadditions.mod.util.crafting.RecipeHandler;
import net.minecraft.item.crafting.IRecipe;
public final class RecipeUtil {
public static LensConversionRecipe lastReconstructorRecipe() {
List<LensConversionRecipe> list = ActuallyAdditionsAPI.RECONSTRUCTOR_LENS_CONVERSION_RECIPES;
return list.get(list.size() - 1);
}
public static CrusherRecipe lastCrusherRecipe() {
List<CrusherRecipe> list = ActuallyAdditionsAPI.CRUSHER_RECIPES;
return list.get(list.size() - 1);
}
public static IRecipe lastIRecipe() {
return RecipeHandler.lastRecipe;
}
public static EmpowererRecipe lastEmpowererRecipe() {
List<EmpowererRecipe> list = ActuallyAdditionsAPI.EMPOWERER_RECIPES;
return list.get(list.size() - 1);
}
}