Fixed undo/redo crashing.

This commit is contained in:
Christian Knaapen
2020-03-15 13:24:13 +01:00
parent f764fac778
commit 74d74390d2
9 changed files with 122 additions and 15 deletions

View File

@@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
version = '1.14.4-2.16'
version = '1.14.4-2.18'
group = 'nl.requios.effortlessbuilding' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'effortlessbuilding'

View File

@@ -46,7 +46,7 @@ public class EffortlessBuilding
{
public static final String MODID = "effortlessbuilding";
public static final String NAME = "Effortless Building";
public static final String VERSION = "1.14.4-2.16";
public static final String VERSION = "1.14.4-2.18";
public static EffortlessBuilding instance;
@@ -149,4 +149,9 @@ public class EffortlessBuilding
public static void log(PlayerEntity player, String msg, boolean actionBar){
player.sendStatusMessage(new StringTextComponent(msg), actionBar);
}
//Log with translation supported, call either on client or server (which then sends a message)
public static void logTranslate(PlayerEntity player, String prefix, String translationKey, String suffix, boolean actionBar){
proxy.logTranslate(player, prefix, translationKey, suffix, actionBar);
}
}

View File

@@ -176,7 +176,7 @@ public class ModeOptions {
}
if (player.world.isRemote && action != ActionEnum.REPLACE && action != ActionEnum.OPEN_MODIFIER_SETTINGS) {
ClientProxy.logTranslate(action.name);
EffortlessBuilding.logTranslate(player, "", action.name, "", true);
}
}
}

View File

@@ -12,6 +12,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootParameterSet;
import nl.requios.effortlessbuilding.BuildConfig;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.helper.FixedStack;
@@ -102,7 +103,7 @@ public class UndoRedo {
//get blockstate from itemstack
BlockState previousBlockState = Blocks.AIR.getDefaultState();
if (itemStack.getItem() instanceof BlockItem) {
previousBlockState = previousBlockStates.get(i);//((ItemBlock) itemStack.getItem()).getBlock().getDefaultState();
previousBlockState = ((BlockItem) itemStack.getItem()).getBlock().getDefaultState();
}
if (player.world.isBlockPresent(coordinate)) {
@@ -111,8 +112,10 @@ public class UndoRedo {
itemStack = findItemStackInInventory(player, previousBlockStates.get(i));
//get blockstate from new itemstack
if (!itemStack.isEmpty() && itemStack.getItem() instanceof BlockItem) {
previousBlockState = previousBlockStates.get(i);//((ItemBlock) itemStack.getItem()).getBlock().getDefaultState();
previousBlockState = ((BlockItem) itemStack.getItem()).getBlock().getDefaultState();
} else {
if (previousBlockStates.get(i).getBlock() != Blocks.AIR)
EffortlessBuilding.logTranslate(player, "", previousBlockStates.get(i).getBlock().getTranslationKey(), " not found in inventory", true);
previousBlockState = Blocks.AIR.getDefaultState();
}
}
@@ -160,7 +163,7 @@ public class UndoRedo {
//get blockstate from itemstack
BlockState newBlockState = Blocks.AIR.getDefaultState();
if (itemStack.getItem() instanceof BlockItem) {
newBlockState = newBlockStates.get(i);//((ItemBlock) itemStack.getItem()).getBlock().getDefaultState();
newBlockState = ((BlockItem) itemStack.getItem()).getBlock().getDefaultState();
}
if (player.world.isBlockPresent(coordinate)) {
@@ -169,8 +172,10 @@ public class UndoRedo {
itemStack = findItemStackInInventory(player, newBlockStates.get(i));
//get blockstate from new itemstack
if (!itemStack.isEmpty() && itemStack.getItem() instanceof BlockItem) {
newBlockState = newBlockStates.get(i);//((ItemBlock) itemStack.getItem()).getBlock().getDefaultState();
newBlockState = ((BlockItem) itemStack.getItem()).getBlock().getDefaultState();
} else {
if (newBlockStates.get(i).getBlock() != Blocks.AIR)
EffortlessBuilding.logTranslate(player, "", newBlockStates.get(i).getBlock().getTranslationKey(), " not found in inventory", true);
newBlockState = Blocks.AIR.getDefaultState();
}
}
@@ -214,13 +219,18 @@ public class UndoRedo {
// then change line 103 back (get state from item)
itemStack = InventoryHelper.findItemStackInInventory(player, blockState.getBlock());
//then anything it drops
if (itemStack.isEmpty()) {
List<ItemStack> itemsDropped = blockState.getDrops(new LootContext.Builder((ServerWorld) player.world));
for (ItemStack itemStackDropped : itemsDropped) {
if (itemStackDropped.getItem() instanceof BlockItem) {
Block block = ((BlockItem) itemStackDropped.getItem()).getBlock();
itemStack = InventoryHelper.findItemStackInInventory(player, block);
//Cannot check drops on clientside because loot tables are server only
if (!player.world.isRemote)
{
List<ItemStack> itemsDropped = Block.getDrops(blockState, (ServerWorld) player.world, BlockPos.ZERO, null);
for (ItemStack itemStackDropped : itemsDropped) {
if (itemStackDropped.getItem() instanceof BlockItem) {
Block block = ((BlockItem) itemStackDropped.getItem()).getBlock();
itemStack = InventoryHelper.findItemStackInInventory(player, block);
}
}
}
}

View File

@@ -72,7 +72,8 @@ public class AddUndoMessage {
PlayerEntity player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
//Add to undo stack clientside
UndoRedo.addUndo(ctx.get().getSender(), new BlockSet(
//Only the appropriate player that needs to add this to the undo stack gets this message
UndoRedo.addUndo(player, new BlockSet(
new ArrayList<BlockPos>() {{add(message.getCoordinate());}},
new ArrayList<BlockState>() {{add(message.getPreviousBlockState());}},
new ArrayList<BlockState>() {{add(message.getNewBlockState());}},

View File

@@ -0,0 +1,79 @@
package nl.requios.effortlessbuilding.network;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.network.NetworkEvent;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.buildmodifier.BlockSet;
import nl.requios.effortlessbuilding.buildmodifier.UndoRedo;
import java.util.ArrayList;
import java.util.function.Supplier;
public class TranslatedLogMessage {
private String prefix;
private String translationKey;
private String suffix;
private boolean actionBar;
public TranslatedLogMessage(){
prefix = "";
translationKey = "";
suffix = "";
actionBar = false;
}
public TranslatedLogMessage(String prefix, String translationKey, String suffix, boolean actionBar) {
this.prefix = prefix;
this.translationKey = translationKey;
this.suffix = suffix;
this.actionBar = actionBar;
}
public String getPrefix() {
return prefix;
}
public String getTranslationKey() {
return translationKey;
}
public String getSuffix() {
return suffix;
}
public boolean isActionBar() {
return actionBar;
}
public static void encode(TranslatedLogMessage message, PacketBuffer buf) {
buf.writeString(message.prefix);
buf.writeString(message.translationKey);
buf.writeString(message.suffix);
buf.writeBoolean(message.actionBar);
}
public static TranslatedLogMessage decode(PacketBuffer buf) {
return new TranslatedLogMessage(buf.readString(), buf.readString(), buf.readString(), buf.readBoolean());
}
public static class Handler
{
public static void handle(TranslatedLogMessage message, Supplier<NetworkEvent.Context> ctx)
{
ctx.get().enqueueWork(() -> {
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
//Received clientside
PlayerEntity player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
EffortlessBuilding.logTranslate(player, message.prefix, message.translationKey, message.suffix, message.actionBar);
}
});
ctx.get().setPacketHandled(true);
}
}
}

View File

@@ -360,7 +360,8 @@ public class ClientProxy implements IProxy {
return world.rayTraceBlocks(new RayTraceContext(start, end, RayTraceContext.BlockMode.COLLIDER, RayTraceContext.FluidMode.NONE, player));
}
public static void logTranslate(String key) {
EffortlessBuilding.log(Minecraft.getInstance().player, I18n.format(key), true);
@Override
public void logTranslate(PlayerEntity player, String prefix, String translationKey, String suffix, boolean actionBar) {
EffortlessBuilding.log(Minecraft.getInstance().player, prefix + I18n.format(translationKey) + suffix, actionBar);
}
}

View File

@@ -13,4 +13,6 @@ public interface IProxy {
void clientSetup(final FMLClientSetupEvent event);
PlayerEntity getPlayerEntityFromContext(Supplier<NetworkEvent.Context> ctx);
void logTranslate(PlayerEntity player, String prefix, String translationKey, String suffix, boolean actionBar);
}

View File

@@ -1,9 +1,13 @@
package nl.requios.effortlessbuilding.proxy;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.PacketDistributor;
import nl.requios.effortlessbuilding.network.PacketHandler;
import nl.requios.effortlessbuilding.network.TranslatedLogMessage;
import java.util.function.Supplier;
@@ -20,4 +24,9 @@ public class ServerProxy implements IProxy {
public PlayerEntity getPlayerEntityFromContext(Supplier<NetworkEvent.Context> ctx){
return ctx.get().getSender();
}
@Override
public void logTranslate(PlayerEntity player, String prefix, String translationKey, String suffix, boolean actionBar) {
PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), new TranslatedLogMessage(prefix, translationKey, suffix, actionBar));
}
}