From ec04f72cc6a7380c7e1dca2091ec10f33f581a18 Mon Sep 17 00:00:00 2001 From: Tschipp Date: Thu, 28 Sep 2017 16:51:55 +0200 Subject: [PATCH] Script Checker Entity/Block Conditional --- .../common/scripting/CarryOnOverride.java | 20 ++++++- .../common/scripting/ScriptChecker.java | 52 ++++++++++--------- .../common/scripting/ScriptReader.java | 2 + 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/main/java/tschipp/carryon/common/scripting/CarryOnOverride.java b/src/main/java/tschipp/carryon/common/scripting/CarryOnOverride.java index a807f4e..7d6ef3c 100644 --- a/src/main/java/tschipp/carryon/common/scripting/CarryOnOverride.java +++ b/src/main/java/tschipp/carryon/common/scripting/CarryOnOverride.java @@ -36,13 +36,31 @@ public class CarryOnOverride private String renderRotation; private String renderScale; + private boolean isBlock; + private boolean isEntity; + - + public boolean isBlock() + { + return isBlock; + } + public void setBlock(boolean isBlock) + { + this.isBlock = isBlock; + } + public boolean isEntity() + { + return isEntity; + } + public void setEntity(boolean isEntity) + { + this.isEntity = isEntity; + } public NBTTagCompound getTypeBlockTag() { return typeBlockTag; diff --git a/src/main/java/tschipp/carryon/common/scripting/ScriptChecker.java b/src/main/java/tschipp/carryon/common/scripting/ScriptChecker.java index 102889c..ccf3707 100644 --- a/src/main/java/tschipp/carryon/common/scripting/ScriptChecker.java +++ b/src/main/java/tschipp/carryon/common/scripting/ScriptChecker.java @@ -23,49 +23,53 @@ public class ScriptChecker @Nullable public static CarryOnOverride inspectBlock(IBlockState state, World world, BlockPos pos, @Nullable NBTTagCompound tag) { - if(!CarryOnConfig.settings.useScripts) + if (!CarryOnConfig.settings.useScripts) return null; - + Block block = state.getBlock(); int meta = block.getMetaFromState(state); Material material = state.getMaterial(); float hardness = state.getBlockHardness(world, pos); float resistance = block.getExplosionResistance(null); NBTTagCompound nbt = tag; - - for(CarryOnOverride override : ScriptReader.OVERRIDES) + + for (CarryOnOverride override : ScriptReader.OVERRIDES) { - if(matchesAll(override, block, meta, material, hardness, resistance, nbt)) - return override; + if (override.isBlock()) + { + if (matchesAll(override, block, meta, material, hardness, resistance, nbt)) + return override; + } } - + return null; } - - + @Nullable public static CarryOnOverride inspectEntity(Entity entity) { - if(!CarryOnConfig.settings.useScripts) + if (!CarryOnConfig.settings.useScripts) return null; - + String name = EntityList.getKey(entity).toString(); float height = entity.height; float width = entity.width; float health = entity instanceof EntityLivingBase ? ((EntityLivingBase) entity).getHealth() : 0.0f; NBTTagCompound tag = new NBTTagCompound(); entity.writeToNBT(tag); - - for(CarryOnOverride override : ScriptReader.OVERRIDES) + + for (CarryOnOverride override : ScriptReader.OVERRIDES) { - if(matchesAll(override, name, height, width, health, tag)) - return override; + if (override.isEntity()) + { + if (matchesAll(override, name, height, width, health, tag)) + return override; + } } - + return null; } - - + public static boolean matchesAll(CarryOnOverride override, String name, float height, float width, float health, NBTTagCompound tag) { boolean matchname = name == null ? true : name.equals(override.getTypeNameEntity()); @@ -73,10 +77,10 @@ public class ScriptChecker boolean matchwidth = ScriptParseHelper.matches(width, override.getTypeWidth()); boolean matchhealth = ScriptParseHelper.matches(health, override.getTypeHealth()); boolean matchnbt = ScriptParseHelper.matches(tag, override.getTypeEntityTag()); - + return (matchname && matchheight && matchwidth && matchhealth && matchnbt); } - + public static boolean matchesAll(CarryOnOverride override, Block block, int meta, Material material, float hardness, float resistance, NBTTagCompound nbt) { boolean matchnbt = ScriptParseHelper.matches(nbt, override.getTypeBlockTag()); @@ -85,19 +89,19 @@ public class ScriptChecker boolean matchmaterial = ScriptParseHelper.matches(material, override.getTypeMaterial()); boolean matchhardness = ScriptParseHelper.matches(hardness, override.getTypeHardness()); boolean matchresistance = ScriptParseHelper.matches(resistance, override.getTypeResistance()); - + return (matchnbt && matchblock && matchmeta && matchmaterial && matchhardness && matchresistance); } - + public static boolean fulfillsConditions(CarryOnOverride override, EntityPlayer player) { boolean achievement = ScriptParseHelper.getAchievementFromString(override.getConditionAchievement()) == null ? true : player.hasAchievement(ScriptParseHelper.getAchievementFromString(override.getConditionAchievement())); - boolean gamemode = ScriptParseHelper.matches(((EntityPlayerMP)player).interactionManager.getGameType().getID(), override.getConditionGamemode()); + boolean gamemode = ScriptParseHelper.matches(((EntityPlayerMP) player).interactionManager.getGameType().getID(), override.getConditionGamemode()); boolean gamestage = Loader.isModLoaded("gamestages") ? (override.getConditionGamestage() != null ? PlayerDataHandler.getStageData(player).hasUnlockedStage(override.getConditionGamestage()) : true) : true; boolean position = ScriptParseHelper.matches(player.getPosition(), override.getConditionPosition()); boolean xp = ScriptParseHelper.matches(player.experienceLevel, override.getConditionXp()); boolean scoreboard = ScriptParseHelper.matchesScore(player, override.getConditionScoreboard()); - + return (achievement && gamemode && gamestage && position && xp && scoreboard); } } diff --git a/src/main/java/tschipp/carryon/common/scripting/ScriptReader.java b/src/main/java/tschipp/carryon/common/scripting/ScriptReader.java index 3202767..cdc63da 100644 --- a/src/main/java/tschipp/carryon/common/scripting/ScriptReader.java +++ b/src/main/java/tschipp/carryon/common/scripting/ScriptReader.java @@ -71,6 +71,7 @@ public class ScriptReader if (block != null) { + override.setBlock(true); JsonElement name = block.get("name"); JsonElement meta = block.get("meta"); JsonElement material = block.get("material"); @@ -93,6 +94,7 @@ public class ScriptReader } else { + override.setEntity(true); JsonElement name = entity.get("name"); JsonElement health = entity.get("health"); JsonElement height = entity.get("height");