Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix general issues with breaking things #281

Merged
merged 3 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
import codechicken.lib.raytracer.IndexedCuboid6;
import codechicken.lib.raytracer.RayTracer;
import codechicken.lib.vec.Cuboid6;
import com.google.common.collect.Lists;
import gregtech.api.GregTechAPI;
import gregtech.api.block.BlockCustomParticle;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.tool.IHammerItem;
import gregtech.api.capability.tool.IScrewdriverItem;
import gregtech.api.capability.tool.IWrenchItem;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.cover.ICoverable;
import gregtech.api.cover.IFacadeCover;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.toolitem.IToolStats;
import gregtech.api.items.toolitem.ToolMetaItem;
import gregtech.api.metatileentity.MetaTileEntity;
Expand Down Expand Up @@ -70,12 +67,13 @@
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("deprecation")
public class BlockMachine extends BlockCustomParticle implements ITileEntityProvider, IFacadeWrapper, IBlockAppearance {

private static final List<IndexedCuboid6> EMPTY_COLLISION_BOX = Lists.newArrayList(new IndexedCuboid6(null, Cuboid6.full));
private static final List<IndexedCuboid6> EMPTY_COLLISION_BOX = Collections.emptyList();
private static final IUnlistedProperty<String> HARVEST_TOOL = new UnlistedStringProperty("harvest_tool");
private static final IUnlistedProperty<Integer> HARVEST_LEVEL = new UnlistedIntegerProperty("harvest_level");
//used for rendering purposes of non-opaque machines like chests and tanks
Expand Down Expand Up @@ -177,6 +175,11 @@ private List<IndexedCuboid6> getCollisionBox(IBlockAccess blockAccess, BlockPos
return collisionList;
}

@Override
public boolean doesSideBlockRendering(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing face) {
return state.isOpaqueCube() && getMetaTileEntity(world, pos) != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have an additional check for if the face to be rendered is obscured by another block?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually a method called by another block to check side rendering.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see

}

@Nonnull
@Override
public ItemStack getPickBlock(@Nonnull IBlockState state, @Nonnull RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player) {
Expand Down Expand Up @@ -338,7 +341,7 @@ public boolean onBlockActivated(@Nonnull World worldIn, @Nonnull BlockPos pos, @

if(itemStack.getItem() instanceof ToolMetaItem<?>) {
IToolStats stats = ((ToolMetaItem<?>) itemStack.getItem()).getItem(itemStack).getToolStats();
stats.onBreakingUse(itemStack);
stats.onBreakingUse(itemStack, worldIn, pos);
}
wrenchItem.damageItem(DamageValues.DAMAGE_FOR_WRENCH, false);
return true;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/gregtech/api/items/toolitem/IToolStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
Expand Down Expand Up @@ -177,10 +178,11 @@ default Set<String> getToolClasses(ItemStack stack) {
default void onCraftingUse(ItemStack stack) {
if (ConfigHolder.toolCraftingSounds && ForgeHooks.getCraftingPlayer() != null && stack.getItem() instanceof ToolMetaItem<?>)
ForgeHooks.getCraftingPlayer().playSound(((ToolMetaItem<?>) stack.getItem()).getItem(stack).getSound(), 1, 1);

}

default void onBreakingUse(ItemStack stack) {
if (ConfigHolder.toolUseSounds && Minecraft.getMinecraft().player != null && stack.getItem() instanceof ToolMetaItem<?>)
Minecraft.getMinecraft().player.playSound(((ToolMetaItem<?>) stack.getItem()).getItem(stack).getSound(), 1, 1);
default void onBreakingUse(ItemStack stack, World world, BlockPos pos) {
if (ConfigHolder.toolUseSounds && stack.getItem() instanceof ToolMetaItem<?>)
world.playSound(null, pos, ((ToolMetaItem<?>) stack.getItem()).getItem(stack).getSound(), SoundCategory.PLAYERS, 1, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public boolean onBlockDestroyed(@Nonnull ItemStack stack, @Nonnull World world,
if (metaToolValueItem != null) {
IToolStats toolStats = metaToolValueItem.getToolStats();
toolStats.onBlockDestroyed(stack, world, state, pos, entity);
toolStats.onBreakingUse(stack);
toolStats.onBreakingUse(stack, world, pos);
damageItem(stack, toolStats.getToolDamagePerBlockBreak(stack), false);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.gui.IUIHolder;
import gregtech.api.net.CPacketRecoverMTE;
import gregtech.api.net.NetworkHandler;
import gregtech.api.util.GTLog;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -49,10 +51,7 @@ public MetaTileEntity setMetaTileEntity(MetaTileEntity sampleMetaTileEntity, Obj
this.metaTileEntity.onAttached();
if (hasWorld() && !getWorld().isRemote) {
updateBlockOpacity();
writeCustomData(INITIALIZE_MTE, buffer -> {
buffer.writeVarInt(GregTechAPI.MTE_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
metaTileEntity.writeInitialSyncData(buffer);
});
sendInitialSyncData();
//just to update neighbours so cables and other things will work properly
this.needToUpdateLightning = true;
world.neighborChanged(getPos(), getBlockType(), getPos());
Expand Down Expand Up @@ -128,7 +127,10 @@ public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFaci
public void update() {
if (metaTileEntity != null) {
metaTileEntity.update();
} else if (world.isRemote) { // recover the mte
NetworkHandler.channel.sendToServer(new CPacketRecoverMTE(world, getPos()).toFMLPacket());
}

if (this.needToUpdateLightning) {
getWorld().checkLight(getPos());
this.needToUpdateLightning = false;
Expand All @@ -138,6 +140,13 @@ public void update() {
super.update();
}

public void sendInitialSyncData() {
writeCustomData(INITIALIZE_MTE, buffer -> {
buffer.writeVarInt(GregTechAPI.MTE_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
metaTileEntity.writeInitialSyncData(buffer);
});
}

@Override
public void writeInitialSyncData(PacketBuffer buf) {
if (metaTileEntity != null) {
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/gregtech/api/net/CPacketRecoverMTE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gregtech.api.net;

import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntityHolder;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;

public class CPacketRecoverMTE implements NetworkHandler.Packet {
public World world;
public BlockPos pos;


public CPacketRecoverMTE(World world, BlockPos pos) {
this.world = world;
this.pos = pos;
}

public static void registerPacket(int packetId) {
NetworkHandler.registerPacket(packetId, CPacketRecoverMTE.class, new NetworkHandler.PacketCodec<>(
(packet, buf) -> {
buf.writeVarInt(packet.world.provider.getDimension());
buf.writeBlockPos(packet.pos);
},
(buf) -> new CPacketRecoverMTE(FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(buf.readVarInt()), buf.readBlockPos())
));
}

public static void registerExecutor() {
NetworkHandler.registerServerExecutor(CPacketRecoverMTE.class, (packet, handler) -> {
TileEntity te = packet.world.getTileEntity(packet.pos);
if (te instanceof MetaTileEntityHolder && ((MetaTileEntityHolder) te).isValid()) {
((MetaTileEntityHolder) te).sendInitialSyncData();
}
});
}

}
34 changes: 6 additions & 28 deletions src/main/java/gregtech/api/net/NetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,29 +223,9 @@ public static void init() {
}
));

registerPacket(7, CPacketPluginSynced.class, new NetworkHandler.PacketCodec<>(
(packet, buf) -> {
MetaTileEntityMonitorScreen screen = packet.plugin.getScreen();
buf.writeVarInt(screen.getWorld().provider.getDimension());
buf.writeBlockPos(screen.getPos());
buf.writeVarInt(packet.id);
if(packet.payloadWriter != null) {
packet.payloadWriter.accept(buf);
}
},
(buf) -> {
int dim = buf.readVarInt();
BlockPos pos = buf.readBlockPos();
TileEntity te = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(dim).getTileEntity(pos);
if(te instanceof MetaTileEntityHolder && ((MetaTileEntityHolder) te).getMetaTileEntity() instanceof MetaTileEntityMonitorScreen) {
MonitorPluginBaseBehavior pluginBaseBehavior = ((MetaTileEntityMonitorScreen) ((MetaTileEntityHolder) te).getMetaTileEntity()).plugin;
if (pluginBaseBehavior != null) {
return new CPacketPluginSynced(pluginBaseBehavior, buf);
}
}
return new CPacketPluginSynced(null, buf);
}
));
CPacketPluginSynced.registerPacket(7);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move these packet numbers to GregtechDataCodes?

Copy link
Contributor

@Yefancy Yefancy Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blueberry said he will rework a general refactoring of NetworkHandler. another pr


CPacketRecoverMTE.registerPacket(8);

registerServerExecutor(PacketUIClientAction.class, (packet, handler) -> {
Container openContainer = handler.player.openContainer;
Expand All @@ -263,11 +243,9 @@ public static void init() {
}
});

registerServerExecutor(CPacketPluginSynced.class, (packet, handler) -> {
if (packet.plugin != null) {
packet.plugin.readPluginAction(handler.player, packet.id, packet.buf);
}
});
CPacketPluginSynced.registerExecutor();

CPacketRecoverMTE.registerExecutor();

if (FMLCommonHandler.instance().getSide().isClient()) {
initClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void fixProblemWithTool(int problemIndex, EntityPlayer entityPlayer) {
for (ItemStack itemStack : entityPlayer.inventory.mainInventory) {
if (itemStack.isItemEqualIgnoreDurability(tool.getStackForm())) {
((IMaintenance) this.getController()).setMaintenanceFixed(problemIndex);
tool.getToolStats().onBreakingUse(itemStack);
tool.getToolStats().onBreakingUse(itemStack, getWorld(), getPos());
damageTool(itemStack);
this.setTaped(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/common/tools/ToolAxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
public void onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos, EntityLivingBase entity) {
super.onBlockDestroyed(stack, world, state, pos, entity);
if (!entity.isSneaking() && entity instanceof EntityPlayer) {
this.onBreakingUse(stack);
this.onBreakingUse(stack, world, pos);
ToolUtility.applyTimberAxe(stack, world, pos, (EntityPlayer) entity);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/common/tools/ToolChainsaw.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public float getMaxDurabilityMultiplier(ItemStack stack) {
public void onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos, EntityLivingBase entity) {
super.onBlockDestroyed(stack, world, state, pos, entity);
if (!entity.isSneaking() && entity instanceof EntityPlayer) {
this.onBreakingUse(stack);
this.onBreakingUse(stack, world, pos);
ToolUtility.applyTimberAxe(stack, world, pos, (EntityPlayer) entity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private static Vec3i multiplyVec(Vec3i start, int multiplier) {
}

@Override
public void onBreakingUse(ItemStack stack) {
public void onBreakingUse(ItemStack stack, World world, BlockPos pos) {
}

@Override
Expand Down