Files
SteamWar/MissileWars/src/de/steamwar/misslewars/items/CustomItem.java
T
2025-10-23 17:56:43 +02:00

85 lines
2.8 KiB
Java

/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.items;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.scripts.ScriptedItem;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
public class CustomItem extends SpecialItem {
private ScriptedItem scriptedItem;
public CustomItem(ScriptedItem scriptedItem) {
this.scriptedItem = scriptedItem;
}
@Override
public ItemStack getItem() {
return scriptedItem.getItemStack();
}
@Override
public boolean handleUse(Player p) {
return scriptedItem.execute(ScriptedItem.EventType.onClick, p, p.getLocation());
}
@Override
public void handleThrow(Entity e) {
scriptedItem.execute(ScriptedItem.EventType.onThrow, e, e.getLocation());
}
@Override
public void handleHit(Entity e, Location l) {
scriptedItem.execute(ScriptedItem.EventType.onHit, e, l);
}
public static void init() {
File itemsFolder = new File(MissileWars.getPlugin().getDataFolder(), "items");
if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) throw new SecurityException("Items could not be loaded");
for (File itemFile : Objects.requireNonNull(itemsFolder.listFiles())) {
if (!itemFile.canRead() || !itemFile.isFile()) continue;
try {
JsonObject jsonObject = new JsonParser().parse(new FileReader(itemFile)).getAsJsonObject();
new CustomItem(new ScriptedItem(jsonObject));
} catch (JsonSyntaxException | IOException e) {
e.printStackTrace();
throw new SecurityException("Item JSON error " + itemFile, e);
}
}
}
public ScriptedItem getScriptedItem() {
return scriptedItem;
}
}