-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added spawner count & spawner entity type properties
- Loading branch information
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/info/itsthesky/itemcreator/core/properties/spawners/SpawnerProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package info.itsthesky.itemcreator.core.properties.spawners; | ||
|
||
import info.itsthesky.itemcreator.ItemCreator; | ||
import info.itsthesky.itemcreator.api.properties.base.BlockProperty; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.block.CreatureSpawner; | ||
|
||
public abstract class SpawnerProperty<T> extends BlockProperty<T> { | ||
|
||
public SpawnerProperty(ItemCreator instance) { | ||
super(instance); | ||
} | ||
|
||
@Override | ||
public void editBlock(Block block, T value) { | ||
update(((CreatureSpawner) block.getState()), value); | ||
} | ||
|
||
public abstract void update(CreatureSpawner spawner, T value); | ||
} |
85 changes: 85 additions & 0 deletions
85
...n/java/info/itsthesky/itemcreator/core/properties/spawners/SpawnerSpawnCountProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package info.itsthesky.itemcreator.core.properties.spawners; | ||
|
||
import com.cryptomorin.xseries.XMaterial; | ||
import info.itsthesky.itemcreator.ItemCreator; | ||
import info.itsthesky.itemcreator.core.CustomItem; | ||
import info.itsthesky.itemcreator.core.gui.EditorGUI; | ||
import info.itsthesky.itemcreator.core.langs.LangLoader; | ||
import info.itsthesky.itemcreator.utils.ChatWaiter; | ||
import org.apache.commons.lang.WordUtils; | ||
import org.bukkit.block.CreatureSpawner; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
public class SpawnerSpawnCountProperty extends SpawnerProperty<Integer> { | ||
public SpawnerSpawnCountProperty(ItemCreator instance) { | ||
super(instance); | ||
} | ||
|
||
@Override | ||
public void update(CreatureSpawner spawner, Integer value) { | ||
spawner.setSpawnCount(value); | ||
} | ||
|
||
@Override | ||
public List<String> isCompatible(CustomItem item) { | ||
final List<String> errors = new ArrayList<>(); | ||
if (!item.getPropertyValue(XMaterial.class, "material").equals(XMaterial.SPAWNER)) | ||
errors.add(LangLoader.get().format("property.material_require", "Mob Spawner")); | ||
errors.addAll(super.isCompatible(item)); | ||
return errors; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "spawner_spawn_count"; | ||
} | ||
|
||
@Override | ||
public boolean allowClearing() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Integer getDefaultValue() { | ||
return 4; | ||
} | ||
|
||
@Override | ||
public void onEditorClick(InventoryClickEvent e, CustomItem item) { | ||
final Player player = (Player) e.getWhoClicked(); | ||
player.closeInventory(); | ||
player.sendMessage(LangLoader.get().format("messages.parsing_number")); | ||
new ChatWaiter(ev -> ev.getPlayer().equals(player), ev -> { | ||
final Integer type = convert(ev.getMessage(), player); | ||
if (type != null) { | ||
item.setPropertyValue(this, type); | ||
save(item, ev.getMessage(), player); | ||
player.sendMessage(LangLoader.get().format("messages.success")); | ||
} | ||
new EditorGUI(item, true).open(player); | ||
}, true, true); | ||
} | ||
|
||
@Override | ||
public XMaterial getMaterial() { | ||
return XMaterial.RAIL; | ||
} | ||
|
||
@Override | ||
public @Nullable Integer convert(String input, Player player) { | ||
try { | ||
return Integer.parseInt(input); | ||
} catch (Exception ex) { | ||
if (player != null) | ||
player.sendMessage(LangLoader.get().format("messages.wrong_number")); | ||
return null; | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/info/itsthesky/itemcreator/core/properties/spawners/SpawnerTypeProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package info.itsthesky.itemcreator.core.properties.spawners; | ||
|
||
import com.cryptomorin.xseries.XMaterial; | ||
import info.itsthesky.itemcreator.ItemCreator; | ||
import info.itsthesky.itemcreator.api.properties.base.BlockProperty; | ||
import info.itsthesky.itemcreator.core.CustomItem; | ||
import info.itsthesky.itemcreator.core.gui.EditorGUI; | ||
import info.itsthesky.itemcreator.core.langs.LangLoader; | ||
import info.itsthesky.itemcreator.utils.ChatWaiter; | ||
import org.apache.commons.lang.WordUtils; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.block.CreatureSpawner; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
public class SpawnerTypeProperty extends SpawnerProperty<EntityType> { | ||
public SpawnerTypeProperty(ItemCreator instance) { | ||
super(instance); | ||
} | ||
|
||
@Override | ||
public void update(CreatureSpawner spawner, EntityType value) { | ||
spawner.setSpawnedType(value); | ||
} | ||
|
||
@Override | ||
public List<String> isCompatible(CustomItem item) { | ||
final List<String> errors = new ArrayList<>(); | ||
if (!item.getPropertyValue(XMaterial.class, "material").equals(XMaterial.SPAWNER)) | ||
errors.add(LangLoader.get().format("property.material_require", "Mob Spawner")); | ||
errors.addAll(super.isCompatible(item)); | ||
return errors; | ||
} | ||
|
||
@Override | ||
public String asString(EntityType value) { | ||
return WordUtils.capitalize(value.name().toLowerCase().replace("_", " ")); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "spawner_type"; | ||
} | ||
|
||
@Override | ||
public boolean allowClearing() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public EntityType getDefaultValue() { | ||
return EntityType.PIG; | ||
} | ||
|
||
@Override | ||
public void onEditorClick(InventoryClickEvent e, CustomItem item) { | ||
final Player player = (Player) e.getWhoClicked(); | ||
player.closeInventory(); | ||
player.sendMessage(LangLoader.get().format("messages.entity_type")); | ||
new ChatWaiter(ev -> ev.getPlayer().equals(player), ev -> { | ||
final EntityType type = convert(ev.getMessage(), player); | ||
if (type != null) { | ||
item.setPropertyValue(this, type); | ||
save(item, ev.getMessage(), player); | ||
player.sendMessage(LangLoader.get().format("messages.success")); | ||
} | ||
new EditorGUI(item, true).open(player); | ||
}, true, true); | ||
} | ||
|
||
@Override | ||
public XMaterial getMaterial() { | ||
return XMaterial.EGG; | ||
} | ||
|
||
@Override | ||
public @Nullable EntityType convert(String input, Player player) { | ||
try { | ||
return EntityType.valueOf(input.toUpperCase(Locale.ROOT).replace(" ", "_")); | ||
} catch (Exception ex) { | ||
if (player != null) | ||
player.sendMessage(LangLoader.get().format("messages.wrong_entity_type")); | ||
return null; | ||
} | ||
} | ||
} |