Prechádzať zdrojové kódy

clean up, mapper functions, new snuvi stuff (tables, tags)

Kajetan Johannes Hammerle 5 rokov pred
rodič
commit
6e35d06209

+ 4 - 3
src/main/java/me/km/blockprotections/BlockProtectionEvents.java

@@ -10,6 +10,7 @@ import net.minecraft.block.DoorBlock;
 import net.minecraft.block.BlockState;
 import net.minecraft.block.Blocks;
 import net.minecraft.state.properties.DoubleBlockHalf;
+import net.minecraft.tags.BlockTags;
 import net.minecraft.util.Hand;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.IWorld;
@@ -91,8 +92,8 @@ public class BlockProtectionEvents
         ModEntityPlayerMP p = (ModEntityPlayerMP) e.getPlayer();
         BlockState state = e.getState();
 
-        BlockPos pos = e.getPos();
-        if(Utils.getStateValue(state, DoorBlock.HALF) == DoubleBlockHalf.UPPER)
+        BlockPos pos = e.getPos();        
+        if(BlockTags.DOORS.contains(state.getBlock()) && state.get(DoorBlock.HALF) == DoubleBlockHalf.UPPER)
         {
             pos = pos.add(0, -1, 0);
         } 
@@ -115,7 +116,7 @@ public class BlockProtectionEvents
         BlockState state = e.getWorld().getBlockState(e.getPos());
         ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
         BlockPos pos = e.getPos();
-        if(Utils.getStateValue(state, DoorBlock.HALF) == DoubleBlockHalf.UPPER)
+        if(BlockTags.DOORS.contains(state.getBlock()) && state.get(DoorBlock.HALF) == DoubleBlockHalf.UPPER)
         {
             pos = pos.add(0, -1, 0);
         }

+ 5 - 1
src/main/java/me/km/effects/EffectUtils.java

@@ -189,8 +189,12 @@ public class EffectUtils
                 launch = new FireballEntity(w, p, v.x, v.y, v.z);
             }
         }
+        else
+        {
+            return null;
+        }
 
-        Utils.scaleVelocity(launch, scale);
+        launch.setMotion(launch.getMotion().scale(scale));
         w.addEntity(launch);
         return (T) launch;
     }

+ 69 - 9
src/main/java/me/km/snuviscript/MinecraftFunctions.java

@@ -50,6 +50,7 @@ import me.km.playerbank.IPlayerBank;
 import me.km.plots.IProtection;
 import me.km.scheduler.SnuviScheduler;
 import me.km.utils.Mapper;
+import me.km.utils.TableUtils;
 import net.minecraft.block.Block;
 import net.minecraft.block.BlockState;
 import net.minecraft.block.ChestBlock;
@@ -76,6 +77,7 @@ import net.minecraft.nbt.CompoundNBT;
 import net.minecraft.nbt.NBTUtil;
 import net.minecraft.network.play.client.CChatMessagePacket;
 import net.minecraft.network.play.client.CClientStatusPacket;
+import net.minecraft.network.play.server.SEntityVelocityPacket;
 import net.minecraft.network.play.server.SSpawnPositionPacket;
 import net.minecraft.network.play.server.STitlePacket;
 import net.minecraft.network.play.server.SUpdateTileEntityPacket;
@@ -85,12 +87,16 @@ import net.minecraft.server.MinecraftServer;
 import net.minecraft.server.management.PlayerList;
 import net.minecraft.server.management.ProfileBanEntry;
 import net.minecraft.state.properties.ChestType;
+import net.minecraft.tags.BlockTags;
+import net.minecraft.tags.ItemTags;
+import net.minecraft.tags.Tag;
 import net.minecraft.tileentity.ChestTileEntity;
 import net.minecraft.tileentity.MobSpawnerTileEntity;
 import net.minecraft.tileentity.SignTileEntity;
 import net.minecraft.tileentity.SkullTileEntity;
 import net.minecraft.util.Direction;
 import net.minecraft.util.NonNullList;
+import net.minecraft.util.ResourceLocation;
 import net.minecraft.util.math.MathHelper;
 import net.minecraft.util.text.ITextComponent;
 import net.minecraft.util.text.StringTextComponent;
@@ -159,7 +165,23 @@ public class MinecraftFunctions
         // ---------------------------------------------------------------------
         // Title-library  
         // ---------------------------------------------------------------------
-        sm.registerFunction("title.settime", (sc, in) -> 
+        sm.registerFunction("table.getstart", (sc, in) -> TableUtils.getTableStart(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc)));
+        sm.registerFunction("title.getmiddle", (sc, in) -> TableUtils.getTableMiddle(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc)));
+        sm.registerFunction("title.getend", (sc, in) -> TableUtils.getTableEnd(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc)));
+        sm.registerFunction("title.get", (sc, in) -> 
+        { 
+            String[] columns = new String[in.length - 2];
+            for(int i = 0; i < columns.length; i++)
+            {
+                columns[i] = in[i + 2].getString(sc);
+            }
+            return TableUtils.getTable(in[0].getInt(sc), in[1].getString(sc), columns);
+        });
+        
+        // ---------------------------------------------------------------------
+        // table library  
+        // ---------------------------------------------------------------------
+        sm.registerFunction("table.settime", (sc, in) -> 
         {
             ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(in[1].getInt(sc), in[2].getInt(sc), in[3].getInt(sc)));
             return Void.TYPE;
@@ -362,7 +384,7 @@ public class MinecraftFunctions
                 return new Location(((PlayerEntity) in[0].get(sc)).world, 
                         Utils.getTargetBlock((PlayerEntity) in[0].get(sc), in[1].getInt(sc), in[2].getBoolean(sc), in[3].getBoolean(sc)));
             }
-            return new Location(((PlayerEntity) in[0].get(sc)).world, Utils.getTargetBlock((PlayerEntity) in[0].get(sc), in[1].getInt(sc)));
+            return new Location(((PlayerEntity) in[0].get(sc)).world, Utils.getTargetBlock((PlayerEntity) in[0].get(sc), in[1].getInt(sc), true, false));
         });
                 
         sm.registerFunction("player.gettargetentity", (sc, in) -> Utils.getTargetedEntity((PlayerEntity) in[0].get(sc), in[1].getDouble(sc), getClass(in[2].getString(sc))));
@@ -654,7 +676,8 @@ public class MinecraftFunctions
             }
             return Void.TYPE; 
         });                  
-        sm.registerFunction("item.gettooltype", (sc, in) -> Mapper.getToolType((ItemStack) in[0].get(sc)));
+        sm.registerFunction("item.gettag", (sc, in) -> ItemTags.getCollection().get(new ResourceLocation(in[0].getString(sc))));
+        sm.registerFunction("item.hastag", (sc, in) -> ((Tag<Item>) in[0].get(sc)).contains(((ItemStack) in[0].get(sc)).getItem()));
         sm.registerFunction("item.get", (sc, in) -> ((ItemStack) in[0].get(sc)).getItem());
         sm.registerFunction("item.gettype", (sc, in) -> ((ItemStack) in[0].get(sc)).getItem().getRegistryName().toString());     
         sm.registerFunction("item.getamount", (sc, in) -> (double) ((ItemStack) in[0].get(sc)).getCount()); 
@@ -867,6 +890,8 @@ public class MinecraftFunctions
         // ---------------------------------------------------------------------    
         // Block-library
         // ---------------------------------------------------------------------    
+        sm.registerFunction("block.gettag", (sc, in) -> BlockTags.getCollection().get(new ResourceLocation(in[0].getString(sc))));
+        sm.registerFunction("block.hastag", (sc, in) -> ((Tag<Block>) in[0].get(sc)).contains((Block) in[0].get(sc)));
         sm.registerFunction("block.gettype", (sc, in) -> 
         {
             Location l = (Location) in[0].get(sc);
@@ -1197,12 +1222,51 @@ public class MinecraftFunctions
         sm.registerFunction("entity.getname", (sc, in) -> ((Entity) in[0].get(sc)).getDisplayName().getString()); 
         sm.registerFunction("entity.throw", (sc, in) -> 
         { 
-            Utils.setVelocity((Entity) in[0].get(sc), in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)); 
+            Entity ent = (Entity) in[0].get(sc);
+            ent.setMotion(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc));
+            if(ent instanceof ServerPlayerEntity)
+            {
+                ServerPlayerEntity p = (ServerPlayerEntity) ent;
+                SEntityVelocityPacket packet = new SEntityVelocityPacket(p);
+                p.connection.sendPacket(packet);
+            }
             return Void.TYPE; 
         });
         sm.registerFunction("entity.teleport", (sc, in) -> 
         { 
-            Utils.teleportEntity((Entity) in[0].get(sc), (Location) in[1].get(sc)); 
+            Entity ent = (Entity) in[0].get(sc);
+            Location l = (Location) in[1].get(sc); 
+            if(ent instanceof ServerPlayerEntity)
+            {
+                ServerPlayerEntity p = (ServerPlayerEntity) ent;
+
+                p.stopRiding();
+                if(p.isSleeping())
+                {
+                    p.wakeUpPlayer(true, true, false);
+                }
+
+                float yaw = l.getYaw() != 0.0f ? l.getYaw() : ent.rotationYaw;
+                float pitch = l.getPitch() != 0.0f ? l.getPitch() : ent.rotationPitch;
+                p.teleport((ServerWorld) l.getWorld(), l.getX(), l.getY(), l.getZ(), yaw, pitch);
+            }
+            else
+            {
+                if(ent.world != l.getWorld())
+                {
+                    ServerWorld ws = (ServerWorld) l.getWorld();
+                    ent.changeDimension(ws.getDimension().getType());
+                }
+                if(l.getYaw() != 0 && l.getPitch() != 0)
+                {
+                    ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
+                }
+                else
+                {
+                    ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
+                }
+            }
+            
             return Void.TYPE; 
         });  
         sm.registerFunction("entity.setequip", (sc, in) -> 
@@ -1937,10 +2001,6 @@ public class MinecraftFunctions
                 String left = SnuviUtils.connect(sc, in, 1);
                 return ItemStackUtils.getStackFromNbtString((s + left).replace('\'', '"'));
             }
-            if(s.indexOf(':') == -1)
-            {
-                s = "minecraft:" + s;
-            }
             Item item = Mapper.getItem(s);
             int amount = in.length >= 2 ? in[1].getInt(sc) : 1;
             ItemStack stack = new ItemStack(item, amount);

+ 10 - 1
src/main/java/me/km/snuviscript/ScriptEvents.java

@@ -785,7 +785,16 @@ public class ScriptEvents
     {    
         handleEvent(e.getPlayer(), "chat", (sc) -> 
         {
-            sc.setVar("message", Utils.colorMessage(perms, e.getMessage(), e.getPlayer()));
+            String text = e.getMessage();
+            if(perms.hasPermission(e.getPlayer(), "color"))
+            {
+                text = text.replace('&', '§');
+            }
+            else
+            {
+                text = text.replaceAll("&.", "");
+            }
+            sc.setVar("message", text);
             sc.setVar("cancel", e.isCanceled());
         }, (sc) -> 
         {

+ 7 - 29
src/main/java/me/km/utils/Mapper.java

@@ -1,59 +1,37 @@
 package me.km.utils;
 
 import net.minecraft.enchantment.Enchantment;
-import net.minecraft.enchantment.Enchantments;
 import net.minecraft.item.Item;
-import net.minecraft.item.ItemStack;
-import net.minecraft.item.Items;
 import net.minecraft.particles.IParticleData;
-import net.minecraft.particles.ParticleTypes;
 import net.minecraft.potion.Effect;
-import net.minecraft.potion.Effects;
+import net.minecraft.util.ResourceLocation;
 import net.minecraft.util.SoundEvent;
-import net.minecraft.util.SoundEvents;
+import net.minecraftforge.registries.ForgeRegistries;
 
 public class Mapper
 {
     public static SoundEvent getSound(String name)
     {
-        return SoundEvents.ENTITY_DROWNED_SWIM;
-    }
-    
-    public static double getToolType(ItemStack stack)
-    {
-        /*
-        if(item instanceof ItemAxe) return 0.0;
-            else if(item instanceof ItemHammer) return 1.0;
-            else if(item instanceof ItemHoe) return 2.0;
-            else if(item instanceof ItemPickaxe) return 3.0;
-            else if(item instanceof ItemSpade) return 4.0;
-            else if(item instanceof ItemBattleAxe) return 5.0;
-            else if(item instanceof ItemDagger) return 6.0;
-            else if(item instanceof ItemSling) return 7.0;
-            else if(item instanceof ItemScepter) return 8.0;
-            else if(item instanceof ItemStick) return 9.0;
-            else if(item instanceof ItemSword) return 10.0;
-        */
-        return -1;
+        return ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(name));
     }
     
     public static Enchantment getEnchantment(String name)
     {
-        return Enchantments.PROTECTION;
+        return ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(name));
     }
     
     public static Effect getPotion(String name)
     {
-        return Effects.ABSORPTION;
+        return ForgeRegistries.POTIONS.getValue(new ResourceLocation(name));
     }
     
     public static IParticleData getParticle(String name)
     {
-        return ParticleTypes.ANGRY_VILLAGER;
+        return (IParticleData) ForgeRegistries.PARTICLE_TYPES.getValue(new ResourceLocation(name));
     }
     
     public static Item getItem(String name)
     {
-        return Items.APPLE;
+        return ForgeRegistries.ITEMS.getValue(new ResourceLocation(name));
     }
 }

+ 0 - 5
src/main/java/me/km/utils/TableUtils.java

@@ -24,11 +24,6 @@ public class TableUtils
         return sb.toString();
     }
     
-    public static String getTable(int width, String color, List<String> args)
-    {
-        return getTable(width, color, args.toArray(new String[args.size()]));
-    }
-    
     public static String getTableMiddle(int width, int columns, String color)
     {
         StringBuilder sb = new StringBuilder(color + "├");

+ 2 - 140
src/main/java/me/km/utils/Utils.java

@@ -1,10 +1,6 @@
 package me.km.utils;
 
-import java.util.Arrays;
 import java.util.List;
-import java.util.stream.Collectors;
-import me.km.permissions.PermissionManager;
-import net.minecraft.block.BlockState;
 import net.minecraft.entity.Entity;
 import net.minecraft.entity.LivingEntity;
 import net.minecraft.entity.player.PlayerEntity;
@@ -12,104 +8,18 @@ import net.minecraft.entity.player.ServerPlayerEntity;
 import net.minecraft.entity.projectile.ArrowEntity;
 import net.minecraft.entity.projectile.DamagingProjectileEntity;
 import net.minecraft.entity.projectile.ThrowableEntity;
-import net.minecraft.network.play.server.SEntityVelocityPacket;
 import net.minecraft.server.MinecraftServer;
 import net.minecraft.util.math.AxisAlignedBB;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.util.math.Vec3d;
 import net.minecraft.world.IWorld;
-import net.minecraft.state.IProperty;
 import net.minecraft.util.DamageSource;
 import net.minecraft.util.math.RayTraceContext;
 import net.minecraft.util.math.RayTraceResult;
-import net.minecraft.world.ServerWorld;
 import net.minecraft.world.World;
 
 public class Utils 
-{    
-    public static String colorMessage(PermissionManager perms, String text, PlayerEntity p) 
-    {
-        if(perms.hasPermission(p, "color"))
-        {
-            text = text.replace('&', '§');
-            return text;
-        }
-        text = text.replaceAll("&.", "");
-        return text;
-    }
-    
-    public static void setVelocity(Entity ent, double x, double y, double z)
-    {
-        ent.setMotion(new Vec3d(x, y, z));
-        updateVelocity(ent);
-    }
-    
-    public static void addVelocity(Entity ent, double x, double y, double z)
-    {
-        ent.setMotion(ent.getMotion().add(x, y, z));
-        updateVelocity(ent);
-    }
-    
-    private static void updateVelocity(Entity ent)
-    {
-        if(ent instanceof ServerPlayerEntity)
-        {
-            ServerPlayerEntity p = (ServerPlayerEntity) ent;
-            SEntityVelocityPacket packet = new SEntityVelocityPacket(p);
-            p.connection.sendPacket(packet);
-        }
-    }
-    
-    public static void scaleVelocity(Entity ent, double scale)
-    {
-        ent.setMotion(ent.getMotion().scale(scale));
-        updateVelocity(ent);
-    }
-    
-    public static <T extends Comparable<T>> T getStateValue(BlockState state, IProperty<T> property)
-    {
-        try
-        {
-            return state.get(property);
-        }
-        catch(IllegalArgumentException ex)
-        {
-            return null;
-        }
-    }
-             
-    public static String formatString(String original)
-    {
-        if(original.length() == 0)
-        {
-            return original;
-        }
-        else if(original.length() == 1)
-        {
-            return original.toUpperCase();
-        }
-        String[] parts = original.split("_");
-        for(int i = 0; i < parts.length; i++)
-        {
-            parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase();
-        }
-        return connectSpaces(parts, 0);
-    }
-   
-    public static String connect(String[] array, int start, String c)
-    {
-        if(start >= array.length)
-        {
-            return "";
-        }
-        return Arrays.stream(array, start, array.length).collect(Collectors.joining(c));
-    }
-    
-    public static String connectSpaces(String[] array, int start)
-    {
-        return connect(array, start, " ");
-    } 
-    
+{                    
     public static PlayerEntity getPlayerFromProjectile(Entity ent)
     {
         if(ent instanceof ArrowEntity)
@@ -135,15 +45,6 @@ public class Utils
             }
             return null;
         }
-        else if(ent instanceof ThrowableEntity)
-        {
-            ThrowableEntity item = (ThrowableEntity) ent;
-            if(item.getThrower() instanceof PlayerEntity)
-            {
-                return (PlayerEntity) item.getThrower();
-            }
-            return null;
-        }
         return null;
     }
     
@@ -160,40 +61,6 @@ public class Utils
         }
         return null;
     }
-
-    public static void teleportEntity(Entity ent, Location l)
-    {
-        if(ent instanceof ServerPlayerEntity)
-        {
-            ServerPlayerEntity p = (ServerPlayerEntity) ent;
-            
-            p.stopRiding();
-            if(p.isSleeping())
-            {
-                p.wakeUpPlayer(true, true, false);
-            }
-            
-            float yaw = l.getYaw() != 0.0f ? l.getYaw() : ent.rotationYaw;
-            float pitch = l.getPitch() != 0.0f ? l.getPitch() : ent.rotationPitch;
-            p.teleport((ServerWorld) l.getWorld(), l.getX(), l.getY(), l.getZ(), yaw, pitch);
-        }
-        else
-        {
-            if(ent.world != l.getWorld())
-            {
-                ServerWorld ws = (ServerWorld) l.getWorld();
-                ent.changeDimension(ws.getDimension().getType());
-            }
-            if(l.getYaw() != 0 && l.getPitch() != 0)
-            {
-                ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
-            }
-            else
-            {
-                ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
-            }
-        }
-    }
     
     private static AxisAlignedBB buildBoxAround(double x, double y, double z, double radius)
     {
@@ -287,7 +154,7 @@ public class Utils
         return Math.max(Math.max(lowerX, lowerY), lowerZ) < Math.min(Math.min(upperX, upperY), upperZ);
     }
     
-    public static Vec3d getTargetVector(Entity ent, double range)
+    private static Vec3d getTargetVector(Entity ent, double range)
     {
         if(range > 128)
         {
@@ -350,11 +217,6 @@ public class Utils
         return new BlockPos(ray.getHitVec());  
     }
     
-    public static BlockPos getTargetBlock(Entity ent, double range)
-    {
-        return getTargetBlock(ent, range, true, false);
-    }
-    
     public static ServerPlayerEntity getPlayerByName(MinecraftServer server, String name)
     {
         String nameLower = name.toLowerCase();