Parcourir la source

new snuvi commands and events

Kajetan Johannes Hammerle il y a 4 ans
Parent
commit
3e23e18d2a

+ 1 - 1
src/main/java/me/km/Server.java

@@ -123,7 +123,7 @@ public class Server {
                 customEventCaller, scriptBank, databank, plotMap, commands);
 
         MinecraftForge.EVENT_BUS.register(new WorldEvents());
-        scripts.startScript("startscript");
+        scheduler.scheduleTask(() -> scripts.startScript("startscript"));
     }
 
     public static void onStop() {

+ 33 - 4
src/main/java/me/km/snuviscript/ScriptEvents.java

@@ -49,6 +49,10 @@ public class ScriptEvents {
         sc.setVar("item", stack);
     }
 
+    private static void setPlayer(Script sc, PlayerEntity p) {
+        sc.setVar("player", p);
+    }
+
     private final Scripts scripts;
     private final MinecraftServer server;
 
@@ -300,9 +304,20 @@ public class ScriptEvents {
             setLivingEntity(sc, e.getEntityLiving());
             sc.setVar("drops", e.getDrops());
             sc.setVar("damage_source", e.getSource());
-            sc.setVar("looting_level", (double) e.getLootingLevel());
+            sc.setVar("looting", (double) e.getLootingLevel());
         }, null);
     }
+    
+    @SubscribeEvent(receiveCanceled = true)
+    public void onLivingExperienceDrop(LivingExperienceDropEvent e) {
+        handleEvent(e, "living_experience_drop", (sc) -> {
+            setLivingEntity(sc, e.getEntityLiving());
+            sc.setVar("experience", (double) e.getDroppedExperience());
+            sc.setVar("original_experience", (double) e.getOriginalExperience());
+        }, (sc) -> {
+            handleVar(sc, "living_experience_drop", "experience", v -> e.setDroppedExperience(v.getInt(sc)));
+        });
+    }
 
     @SubscribeEvent(receiveCanceled = true)
     public void onProjectileHit(ProjectileImpactEvent e) {
@@ -352,10 +367,24 @@ public class ScriptEvents {
         }
     }
 
+    @SubscribeEvent(receiveCanceled = true)
+    public void onBlockHarvest(BlockEvent.HarvestDropsEvent e) {
+        handleEvent(e, "block_harvest", (sc) -> {
+            setPlayer(sc, e.getHarvester());
+            sc.setVar("location", new Location(e.getWorld(), e.getPos()));
+            sc.setVar("drop_chance", (double) e.getDropChance());
+            sc.setVar("drops", e.getDrops());
+            sc.setVar("fortune", (double) e.getFortuneLevel());
+            sc.setVar("silktouch", e.isSilkTouching());
+        }, (sc) -> {
+            handleVar(sc, "block_harvest", "drop_chance", v -> e.setDropChance(v.getFloat(sc)));
+        });
+    }
+
     @SubscribeEvent(receiveCanceled = true)
     public void onBlockBreak(BlockEvent.BreakEvent e) {
         handleEvent(e.getPlayer(), "block_break", (sc) -> {
-            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos());
+            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos(), e.getState());
             sc.setVar("cancel", e.isCanceled());
         }, (sc) -> {
             simpleCancel(sc, e, "block_break");
@@ -369,7 +398,7 @@ public class ScriptEvents {
         }
         handleEvent((PlayerEntity) e.getEntity(), "block_place", (sc) -> {
             sc.setVar("block_type_after", e.getPlacedBlock().getBlock().getRegistryName());
-            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos());
+            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos(), e.getState());
             sc.setVar("cancel", e.isCanceled());
         }, (sc) -> {
             simpleCancel(sc, e, "block_place");
@@ -645,7 +674,7 @@ public class ScriptEvents {
     @SubscribeEvent(receiveCanceled = true)
     public void onEntityJoinWorld(EntityJoinWorldEvent e) {
         Entity ent = e.getEntity();
-        if(!scripts.getEntityLimits().isAllowedToSpawn(ent.getType())) {
+        if(!e.getEntity().isPassenger() && !scripts.getEntityLimits().isAllowedToSpawn(ent.getType())) {
             e.getEntity().getPassengers().forEach(rider -> {
                 if(rider == null || rider instanceof PlayerEntity) {
                     return;

+ 17 - 10
src/main/java/me/km/snuviscript/ScriptVars.java

@@ -10,22 +10,29 @@ import net.minecraft.world.IWorld;
 
 public class ScriptVars {
     @SuppressWarnings("")
-    public static void setBlockVars(Script qd, IWorld w, BlockPos pos) {
+    public static void setBlockVars(Script sc, IWorld w, BlockPos pos, BlockState state) {
+        sc.setVar("block_loc", new Location(w, pos));
+        sc.setVar("block_type", state.getBlock().getRegistryName().toString());
+        sc.setVar("block", state.getBlock());
+    }
+    
+    @SuppressWarnings("")
+    public static void setBlockVars(Script sc, IWorld w, BlockPos pos) {
         BlockState state = w.getBlockState(pos);
-        qd.setVar("block_loc", new Location(w, pos));
-        qd.setVar("block_type", state.getBlock().getRegistryName().toString());
-        qd.setVar("block", state.getBlock());
+        sc.setVar("block_loc", new Location(w, pos));
+        sc.setVar("block_type", state.getBlock().getRegistryName().toString());
+        sc.setVar("block", state.getBlock());
     }
 
-    public static void setPlayerVars(Script qd, PlayerEntity p) {
-        qd.setVar("player", p);
-        qd.setVar("player_name", p == null ? null : p.getName().getFormattedText());
+    public static void setPlayerVars(Script sc, PlayerEntity p) {
+        sc.setVar("player", p);
+        sc.setVar("player_name", p == null ? null : p.getName().getFormattedText());
     }
 
-    public static void setSecPlayer(Script qd, PlayerEntity p) {
+    public static void setSecPlayer(Script sc, PlayerEntity p) {
         if(p != null) {
-            qd.setVar("sec_player", p);
-            qd.setVar("sec_player_name", p.getName().getFormattedText());
+            sc.setVar("sec_player", p);
+            sc.setVar("sec_player_name", p.getName().getFormattedText());
         }
     }
 

+ 50 - 2
src/main/java/me/km/snuviscript/commands/EnchantmentCommands.java

@@ -1,11 +1,20 @@
 package me.km.snuviscript.commands;
 
+import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 import me.hammerle.snuviscript.code.ScriptManager;
 import me.km.utils.Mapper;
 import net.minecraft.enchantment.Enchantment;
+import net.minecraft.enchantment.EnchantmentData;
 import net.minecraft.enchantment.EnchantmentHelper;
+import net.minecraft.item.EnchantedBookItem;
 import net.minecraft.item.ItemStack;
+import net.minecraft.item.Items;
+import net.minecraft.nbt.CompoundNBT;
+import net.minecraft.nbt.ListNBT;
+import net.minecraft.util.ResourceLocation;
+import net.minecraftforge.registries.ForgeRegistries;
 
 public class EnchantmentCommands {
     public static void registerFunctions(ScriptManager sm) {
@@ -18,10 +27,49 @@ public class EnchantmentCommands {
             return (double) EnchantmentHelper.getEnchantmentLevel((Enchantment) in[0].get(sc), (ItemStack) in[1].get(sc));
         });
         sm.registerFunction("enchantment.readfromitem", (sc, in) -> {
-            return EnchantmentHelper.getEnchantments((ItemStack) in[0].get(sc));
+            return getEnchantments((ItemStack) in[0].get(sc));
         });
         sm.registerConsumer("enchantment.writetoitem", (sc, in) -> {
-            EnchantmentHelper.setEnchantments((Map<Enchantment, Integer>) in[0].get(sc), (ItemStack) in[1].get(sc));
+            setEnchantments((Map<Enchantment, Double>) in[0].get(sc), (ItemStack) in[1].get(sc));
         });
     }
+
+    private static Map<Enchantment, Double> getEnchantments(ItemStack stack) {
+        ListNBT listnbt = stack.getItem() == Items.ENCHANTED_BOOK ? EnchantedBookItem.getEnchantments(stack) : stack.getEnchantmentTagList();
+        return listToMap(listnbt);
+    }
+
+    private static Map<Enchantment, Double> listToMap(ListNBT list) {
+        Map<Enchantment, Double> map = new LinkedHashMap<>();
+        for(int i = 0; i < list.size(); ++i) {
+            CompoundNBT c = list.getCompound(i);
+            Enchantment ench = ForgeRegistries.ENCHANTMENTS.getValue(ResourceLocation.tryCreate(c.getString("id")));
+            if(ench != null) {
+                map.put(ench, (double) c.getInt("lvl"));
+            }
+        }
+        return map;
+    }
+
+    private static void setEnchantments(Map<Enchantment, Double> enchMap, ItemStack stack) {
+        ListNBT list = new ListNBT();
+        for(Entry<Enchantment, Double> entry : enchMap.entrySet()) {
+            Enchantment ench = entry.getKey();
+            if(ench != null) {
+                int i = entry.getValue().intValue();
+                CompoundNBT c = new CompoundNBT();
+                c.putString("id", String.valueOf(ForgeRegistries.ENCHANTMENTS.getKey(ench)));
+                c.putShort("lvl", (short) i);
+                list.add(c);
+                if(stack.getItem() == Items.ENCHANTED_BOOK) {
+                    EnchantedBookItem.addEnchantment(stack, new EnchantmentData(ench, i));
+                }
+            }
+        }
+        if(list.isEmpty()) {
+            stack.removeChildTag("Enchantments");
+        } else if(stack.getItem() != Items.ENCHANTED_BOOK) {
+            stack.setTagInfo("Enchantments", list);
+        }
+    }
 }

+ 1 - 4
src/main/java/me/km/snuviscript/commands/EntityCommands.java

@@ -213,7 +213,7 @@ public class EntityCommands {
         sm.registerConsumer("entity.setinvisible", (sc, in) -> {
             ((Entity) in[0].get(sc)).setInvisible(in[1].getBoolean(sc));
         });
-        sm.registerConsumer("entity.ride", (sc, in) -> {
+        sm.registerConsumer("entity.mount", (sc, in) -> {
             ((Entity) in[0].get(sc)).startRiding(((Entity) in[1].get(sc)));
         });
         sm.registerConsumer("entity.unmount", (sc, in) -> {
@@ -257,9 +257,6 @@ public class EntityCommands {
         sm.registerConsumer("entity.setpickupdelay", (sc, in) -> {
             ((ItemEntity) in[0].get(sc)).setPickupDelay(in[1].getInt(sc));
         });
-        sm.registerConsumer("entity.setage", (sc, in) -> {
-            ReflectionUtils.setAge((ItemEntity) in[0].get(sc), in[1].getInt(sc));
-        });
         sm.registerFunction("entity.spawn", (sc, in) -> {
             ResourceLocation rl = new ResourceLocation(in[0].getString(sc));
             Location l = (Location) in[1].get(sc);

+ 1 - 1
src/main/java/me/km/snuviscript/commands/InventoryCommands.java

@@ -34,7 +34,7 @@ public class InventoryCommands {
             ((IInventory) in[0].get(sc)).setInventorySlotContents(in[1].getInt(sc), (ItemStack) in[2].get(sc));
         });
         sm.registerFunction("inv.getitem", (sc, in) -> ((IInventory) in[0].get(sc)).getStackInSlot(in[1].getInt(sc)));
-        sm.registerFunction("inv.getsize", (sc, in) -> ((IInventory) in[0].get(sc)).getSizeInventory());
+        sm.registerFunction("inv.getsize", (sc, in) -> (double) ((IInventory) in[0].get(sc)).getSizeInventory());
         sm.registerConsumer("inv.open", (sc, in) -> {
             CustomContainer.openForPlayer((ServerPlayerEntity) in[1].get(sc), (ModInventory) in[0].get(sc), in[2].getString(sc), sc);
         });

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

@@ -2,6 +2,7 @@ package me.km.snuviscript.commands;
 
 import me.hammerle.snuviscript.code.ScriptManager;
 import me.km.utils.Location;
+import net.minecraft.entity.EntityType;
 import net.minecraft.entity.item.ItemEntity;
 import net.minecraft.item.ItemStack;
 
@@ -13,11 +14,19 @@ public class ItemEntityCommands {
         });
         sm.registerFunction("item.entity.new", (sc, in) -> {
             Location l = (Location) in[0].get(sc);
-            return new ItemEntity(l.getWorld().getWorld(), l.getX(), l.getY(), l.getZ(), (ItemStack) in[1].get(sc));
+            ItemEntity item = new ItemEntity(EntityType.ITEM, l.getWorld().getWorld());
+            item.setPosition(l.getX(), l.getY(), l.getZ());
+            ItemStack stack = (ItemStack) in[1].get(sc);
+            item.setItem(stack);
+            item.lifespan = stack.getEntityLifespan(l.getWorld().getWorld());
+            return item;
         });
         sm.registerConsumer("item.entity.spawn", (sc, in) -> {
             ItemEntity ent = (ItemEntity) in[0].get(sc);
             ent.world.addEntity(ent);
         });
+        sm.registerConsumer("item.entity.setlifespan", (sc, in) -> {
+            ((ItemEntity) in[0].get(sc)).lifespan = in[1].getInt(sc);
+        });
     }
 }

+ 7 - 4
src/main/java/me/km/snuviscript/commands/ParticleCommands.java

@@ -1,22 +1,25 @@
 package me.km.snuviscript.commands;
 
+import java.util.ArrayList;
 import me.hammerle.snuviscript.code.ScriptManager;
 import me.km.utils.Location;
 import me.km.utils.Mapper;
 import net.minecraft.item.ItemStack;
 import net.minecraft.particles.*;
 import net.minecraft.world.server.ServerWorld;
+import net.minecraftforge.registries.ForgeRegistries;
 
 public class ParticleCommands {
     public static void registerFunctions(ScriptManager sm) {
+        sm.registerFunction("particle.getall", (sc, in) -> new ArrayList<>(ForgeRegistries.PARTICLE_TYPES.getValues()));
         sm.registerFunction("particle.get", (sc, in) -> {
-            IParticleData data = Mapper.getParticle(in[0].getString(sc));
+            ParticleType data = Mapper.getParticle(in[0].getString(sc));
             if(data == ParticleTypes.BLOCK || data == ParticleTypes.FALLING_DUST) {
-                data = new BlockParticleData((ParticleType<BlockParticleData>) data, Mapper.getBlock(in[1].getString(sc)).getDefaultState());
+                return new BlockParticleData((ParticleType<BlockParticleData>) data, Mapper.getBlock(in[1].getString(sc)).getDefaultState());
             } else if(data == ParticleTypes.DUST) {
-                data = new RedstoneParticleData(in[1].getFloat(sc), in[2].getFloat(sc), in[3].getFloat(sc), in[4].getFloat(sc));
+                return new RedstoneParticleData(in[1].getFloat(sc), in[2].getFloat(sc), in[3].getFloat(sc), in[4].getFloat(sc));
             } else if(data == ParticleTypes.ITEM) {
-                data = new ItemParticleData((ParticleType<ItemParticleData>) data, new ItemStack(Mapper.getItem(in[1].getString(sc))));
+                return new ItemParticleData((ParticleType<ItemParticleData>) data, new ItemStack(Mapper.getItem(in[1].getString(sc))));
             }
             return data;
         });

+ 3 - 2
src/main/java/me/km/utils/Mapper.java

@@ -4,6 +4,7 @@ import net.minecraft.block.Block;
 import net.minecraft.enchantment.Enchantment;
 import net.minecraft.item.Item;
 import net.minecraft.particles.IParticleData;
+import net.minecraft.particles.ParticleType;
 import net.minecraft.potion.Effect;
 import net.minecraft.state.IProperty;
 import net.minecraft.state.properties.BlockStateProperties;
@@ -35,8 +36,8 @@ public class Mapper {
         return ForgeRegistries.POTIONS.getValue(new ResourceLocation(name));
     }
 
-    public static IParticleData getParticle(String name) {
-        return (IParticleData) ForgeRegistries.PARTICLE_TYPES.getValue(new ResourceLocation(name));
+    public static ParticleType getParticle(String name) {
+        return ForgeRegistries.PARTICLE_TYPES.getValue(new ResourceLocation(name));
     }
 
     public static Item getItem(String name) {

+ 0 - 13
src/main/java/me/km/utils/ReflectionUtils.java

@@ -178,19 +178,6 @@ public class ReflectionUtils {
         }
     }
 
-    // -----------------------------------------------------------------------------------
-    // random field gets
-    // -----------------------------------------------------------------------------------
-    private final static Field ENTITY_ITEM_AGE = getField(ItemEntity.class, "field_70292_b"); // age
-
-    public static int getAge(ItemEntity item) {
-        return getInt(item, ENTITY_ITEM_AGE, 0);
-    }
-
-    public static void setAge(ItemEntity item, int age) {
-        setInt(item, ENTITY_ITEM_AGE, age);
-    }
-
     // -----------------------------------------------------------------------------------
     // minecraft server 
     // -----------------------------------------------------------------------------------