Changes

Update to 1.17
Line 45: Line 45:  
     },
 
     },
 
     {
 
     {
       "condition": "block_state_property",
+
       "condition": "minecraft:block_state_property",
       "block":"minecraft:wheat"
+
       "block": "minecraft:wheat"
 
     }
 
     }
 
   ],
 
   ],
Line 70: Line 70:  
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
private static class WheatSeedsConverterModifier extends LootModifier {
 
private static class WheatSeedsConverterModifier extends LootModifier {
private final int numSeedsToConvert;
+
  private final int numSeedsToConvert;
private final Item itemToCheck;
+
  private final Item itemToCheck;
private final Item itemReward;
+
  private final Item itemReward;
public WheatSeedsConverterModifier(ILootCondition[] conditionsIn, int numSeeds, Item itemCheck, Item reward) {
+
  public WheatSeedsConverterModifier(LootItemCondition[] conditions, int numSeeds, Item itemCheck, Item reward) {
super(conditionsIn);
+
    super(conditions);
numSeedsToConvert = numSeeds;
+
    numSeedsToConvert = numSeeds;
itemToCheck = itemCheck;
+
    itemToCheck = itemCheck;
itemReward = reward;
+
    itemReward = reward;
}
+
  }
   −
@Nonnull
+
  @Nonnull
@Override
+
  @Override
public List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
+
  public List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
/*
+
    /*
* Additional conditions can be checked, though as much as possible should be parameterized via JSON data.
+
    * Additional conditions can be checked, though as much as possible should be parameterized via JSON data.
* It is better to write a new ILootCondition implementation than to do things here.
+
    * It is better to write a new LootItemCondition implementation than to do things here.
*/
+
    */
int numSeeds = 0;
+
    int numSeeds = 0;
for(ItemStack stack : generatedLoot) {
+
    for(ItemStack stack : generatedLoot) {
if(stack.getItem() == itemToCheck)
+
      if(stack.getItem() == itemToCheck)
numSeeds+=stack.getCount();
+
        numSeeds += stack.getCount();
}
+
    }
if(numSeeds >= numSeedsToConvert) {
+
    if(numSeeds >= numSeedsToConvert) {
generatedLoot.removeIf(x -> x.getItem() == itemToCheck);
+
      generatedLoot.removeIf(x -> x.getItem() == itemToCheck);
generatedLoot.add(new ItemStack(itemReward, (numSeeds/numSeedsToConvert)));
+
      generatedLoot.add(new ItemStack(itemReward, (numSeeds/numSeedsToConvert)));
numSeeds = numSeeds%numSeedsToConvert;
+
      numSeeds = numSeeds % numSeedsToConvert;
if(numSeeds > 0)
+
      if(numSeeds > 0)
generatedLoot.add(new ItemStack(itemToCheck, numSeeds));
+
        generatedLoot.add(new ItemStack(itemToCheck, numSeeds));
}
+
    }
return generatedLoot;
+
    return generatedLoot;
}
+
  }
   −
private static class Serializer extends GlobalLootModifierSerializer<WheatSeedsConverterModifier> {
+
  private static class Serializer extends GlobalLootModifierSerializer<WheatSeedsConverterModifier> {
   −
@Override
+
    @Override
public WheatSeedsConverterModifier read(ResourceLocation name, JsonObject object, ILootCondition[] conditionsIn) {
+
    public WheatSeedsConverterModifier read(ResourceLocation name, JsonObject object, LootItemCondition[] conditions) {
int numSeeds = JSONUtils.getInt(object, "numSeeds");
+
      int numSeeds = GsonHelper.getAsInt(object, "numSeeds");
Item seed = ForgeRegistries.ITEMS.getValue(new ResourceLocation((JSONUtils.getString(object, "seedItem"))));
+
      Item seed = ForgeRegistries.ITEMS.getValue(new ResourceLocation((GsonHelper.getAsString(object, "seedItem"))));
Item wheat = ForgeRegistries.ITEMS.getValue(new ResourceLocation(JSONUtils.getString(object, "replacement")));
+
      Item wheat = ForgeRegistries.ITEMS.getValue(new ResourceLocation(GsonHelper.getAsString(object, "replacement")));
return new WheatSeedsConverterModifier(conditionsIn, numSeeds, seed, wheat);
+
      return new WheatSeedsConverterModifier(conditions, numSeeds, seed, wheat);
}
+
    }
}
+
 
 +
    @Override
 +
    public JsonObject write(WheatSeedsConverterModifier instance) {
 +
      JsonObject json = makeConditions(instance.conditions);
 +
      json.addProperty("numSeeds", instance.numSeedsToConvert);
 +
      json.addProperty("seedItem", ForgeRegistries.ITEMS.getKey(instance.itemToCheck).toString());
 +
      json.addProperty("replacement", ForgeRegistries.ITEMS.getKey(instance.itemReward).toString());
 +
      return json;
 +
    }
 +
  }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 117: Line 126:  
The critical portion is the <code>doApply</code> method.
 
The critical portion is the <code>doApply</code> method.
   −
This method is only called if the <code>conditions</code> specified return <code>true</code> and the modder is now able to make the modifications they desire. In this case we can see that the number of <code>itemToCheck</code> meets or exceeds the <code>numSeedsToConvert</code> before modifying the list by adding an <code>itemReward</code> and removing any excess <code>itemToCheck</code> stacks, matching the previously mentioned effects: When a wheat block is harvested with shears, if enough seeds are generated as loot, they are converted to additional wheat instead.
+
This method is only called if the <code>conditions</code> specified return <code>true</code> and the modder is now able to make the modifications they desire. In this case we can see that the number of <code>itemToCheck</code> meets or exceeds the <code>numSeedsToConvert</code> before modifying the list by adding an <code>itemReward</code> and removing any excess <code>itemToCheck</code> stacks, matching the previously mentioned effects: ''When a wheat block is harvested with shears, if enough seeds are generated as loot, they are converted to additional wheat instead''.
   −
Also take note of the <code>read</code> method in the serializer. The conditions are already deserialized for you and if you have no other data, simply <code>return new MyModifier(conditionsIn)</code>. However, the full <code>JsonObject</code> is available if needed.
+
Also take note of the <code>read</code> method in the serializer. The conditions are already deserialized for you and if you have no other data, simply <code>return new MyModifier(conditions)</code>. However, the full <code>JsonObject</code> is available if needed. The <code>write</code> method, on the other hand, is used for if you want to utilize <code>GlobalLootModifierProvider</code> for [[datageneration|data generation]].
   −
Additional [https://github.com/MinecraftForge/MinecraftForge/blob/1.15.x/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java examples] can be found on the Forge Git repository, including silk touch and smelting effects.
+
Additional [https://github.com/MinecraftForge/MinecraftForge/blob/1.17.x/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java examples] can be found on the Forge Git repository, including silk touch and smelting effects.