Skip to content

Commit

Permalink
Boats+ (SkriptLang#7055)
Browse files Browse the repository at this point in the history
* Starter Commit

* Fix Chest Boat Checks

* One more :'(

* Requested Changes

* Requested Changes - 2

Changed, syntax patterns are now automatic
Changed, to jetbrains nullable
Changed, automatic item comparison
Fixed, inability to spawn chest boats

* Fixes

Added, Nullable annotation
Fixed, Test for entities
Changed, parameter names
Removed, 'fireball' and 'wind charge' entities for test

* Requested Changes

Changed, bamboo pattern getter
Changed, patterns in lang
Added, expected/got to assert

* Requested Changes

* Cleanup

* Filler

* Fix Conflict
  • Loading branch information
TheAbsolutionism authored Oct 13, 2024
1 parent 790c779 commit 1993ab6
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 151 deletions.
89 changes: 41 additions & 48 deletions src/main/java/ch/njol/skript/entity/BoatChestData.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.entity;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser;
import org.bukkit.Material;
import org.bukkit.TreeSpecies;
import org.bukkit.entity.Boat;
import org.bukkit.entity.ChestBoat;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;
import java.util.Random;

public class BoatChestData extends EntityData<ChestBoat> {

private static final Boat.Type[] types = Boat.Type.values();

static {
// This ensures all boats are registered
// As well as in the correct order via 'ordinal'
String[] patterns = new String[types.length + 2];
patterns[0] = "chest boat";
patterns[1] = "any chest boat";
for (Boat.Type boat : types) {
String boatName;
if (boat == Boat.Type.BAMBOO)
boatName = "bamboo chest raft";
else
boatName = boat.toString().replace("_", " ").toLowerCase(Locale.ENGLISH) + " chest boat";
patterns[boat.ordinal() + 2] = boatName;
}

if (Skript.classExists("org.bukkit.entity.ChestBoat")) {
EntityData.register(BoatChestData.class, "chest boat", ChestBoat.class, 0,
"chest boat", "any chest boat", "oak chest boat", "spruce chest boat", "birch chest boat",
"jungle chest boat", "acacia chest boat", "dark oak chest boat");
EntityData.register(BoatChestData.class, "chest boat", ChestBoat.class, 0, patterns);
}
}

public BoatChestData() {
this(0);
}

public BoatChestData(@Nullable TreeSpecies type) {
public BoatChestData(@Nullable Boat.Type type) {
this(type != null ? type.ordinal() + 2 : 1);
}

Expand All @@ -59,23 +54,23 @@ protected boolean init(Literal<?>[] exprs, int matchedPattern, SkriptParser.Pars
}

@Override
protected boolean init(@Nullable Class<? extends ChestBoat> c, @Nullable ChestBoat e) {
if (e != null)
matchedPattern = 2 + e.getWoodType().ordinal();
protected boolean init(@Nullable Class<? extends ChestBoat> clazz, @Nullable ChestBoat entity) {
if (entity != null)
matchedPattern = 2 + entity.getBoatType().ordinal();
return true;
}

@Override
public void set(ChestBoat entity) {
if (matchedPattern == 1) // If the type is 'any boat'.
matchedPattern += new Random().nextInt(TreeSpecies.values().length); // It will spawn a random boat type in case is 'any boat'.
matchedPattern += new Random().nextInt(Boat.Type.values().length); // It will spawn a random boat type in case is 'any boat'.
if (matchedPattern > 1) // 0 and 1 are excluded
entity.setWoodType(TreeSpecies.values()[matchedPattern - 2]); // Removes 2 to fix the index.
entity.setBoatType(Boat.Type.values()[matchedPattern - 2]); // Removes 2 to fix the index.
}

@Override
protected boolean match(ChestBoat entity) {
return matchedPattern <= 1 || entity.getWoodType().ordinal() == matchedPattern - 2;
return matchedPattern <= 1 || entity.getBoatType().ordinal() == matchedPattern - 2;
}

@Override
Expand All @@ -95,34 +90,32 @@ protected int hashCode_i() {

@Override
protected boolean equals_i(EntityData<?> obj) {
if (obj instanceof BoatData)
return matchedPattern == ((BoatData) obj).matchedPattern;
if (obj instanceof BoatChestData boatChestData)
return matchedPattern == boatChestData.matchedPattern;
return false;
}

@Override
public boolean isSupertypeOf(EntityData<?> e) {
if (e instanceof BoatData)
return matchedPattern <= 1 || matchedPattern == ((BoatData) e).matchedPattern;
public boolean isSupertypeOf(EntityData<?> entity) {
if (entity instanceof BoatChestData boatChestData)
return matchedPattern <= 1 || matchedPattern == boatChestData.matchedPattern;
return false;
}

public boolean isOfItemType(ItemType itemType) {
int ordinal = -1;

Material type = itemType.getMaterial();
if (type == Material.OAK_CHEST_BOAT)
Material material = itemType.getMaterial();
if (material == Material.OAK_CHEST_BOAT) {
ordinal = 0;
else if (type == Material.SPRUCE_CHEST_BOAT)
ordinal = TreeSpecies.REDWOOD.ordinal();
else if (type == Material.BIRCH_CHEST_BOAT)
ordinal = TreeSpecies.BIRCH.ordinal();
else if (type == Material.JUNGLE_CHEST_BOAT)
ordinal = TreeSpecies.JUNGLE.ordinal();
else if (type == Material.ACACIA_CHEST_BOAT)
ordinal = TreeSpecies.ACACIA.ordinal();
else if (type == Material.DARK_OAK_CHEST_BOAT)
ordinal = TreeSpecies.DARK_OAK.ordinal();
} else {
for (Boat.Type boat : types) {
if (material.name().contains(boat.toString())) {
ordinal = boat.ordinal();
break;
}
}
}
return hashCode_i() == ordinal + 2 || (matchedPattern + ordinal == 0) || ordinal == 0;
}

Expand Down
100 changes: 43 additions & 57 deletions src/main/java/ch/njol/skript/entity/BoatData.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.entity;

import java.lang.reflect.Method;
import java.util.Locale;
import java.util.Random;

import org.bukkit.Material;
import org.bukkit.TreeSpecies;
import org.bukkit.entity.Boat;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import org.jetbrains.annotations.Nullable;

public class BoatData extends EntityData<Boat> {

private static final Boat.Type[] types = Boat.Type.values();

static {
// It will only register for 1.10+,
// See SimpleEntityData if 1.9 or lower.
if (Skript.methodExists(Boat.class, "getWoodType")) { //The 'boat' is the same of 'oak boat', 'any boat' works as supertype and it can spawn random boat.
EntityData.register(BoatData.class, "boat", Boat.class, 0,
"boat", "any boat", "oak boat", "spruce boat", "birch boat", "jungle boat", "acacia boat", "dark oak boat");
// This ensures all boats are registered
// As well as in the correct order via 'ordinal'
String[] patterns = new String[types.length + 2];
patterns[0] = "chest boat";
patterns[1] = "any chest boat";
for (Boat.Type boat : types) {
String boatName;
if (boat == Boat.Type.BAMBOO)
boatName = "bamboo raft";
else
boatName = boat.toString().replace("_", " ").toLowerCase(Locale.ENGLISH) + " boat";
patterns[boat.ordinal() + 2] = boatName;
}
EntityData.register(BoatData.class, "boat", Boat.class, 0, patterns);
}

public BoatData(){
this(0);
}
public BoatData(@Nullable TreeSpecies type){

public BoatData(@Nullable Boat.Type type){
this(type != null ? type.ordinal() + 2 : 1);
}

Expand All @@ -62,23 +50,24 @@ protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parse
}

@Override
protected boolean init(@Nullable Class<? extends Boat> c, @Nullable Boat e) {
if (e != null)
matchedPattern = 2 + e.getWoodType().ordinal();
protected boolean init(@Nullable Class<? extends Boat> clazz, @Nullable Boat entity) {
if (entity != null)

matchedPattern = 2 + entity.getBoatType().ordinal();
return true;
}

@Override
public void set(Boat entity) {
if (matchedPattern == 1) // If the type is 'any boat'.
matchedPattern += new Random().nextInt(TreeSpecies.values().length); // It will spawn a random boat type in case is 'any boat'.
matchedPattern += new Random().nextInt(Boat.Type.values().length); // It will spawn a random boat type in case is 'any boat'.
if (matchedPattern > 1) // 0 and 1 are excluded
entity.setWoodType(TreeSpecies.values()[matchedPattern - 2]); // Removes 2 to fix the index.
entity.setBoatType(Boat.Type.values()[matchedPattern - 2]); // Removes 2 to fix the index.
}

@Override
protected boolean match(Boat entity) {
return matchedPattern <= 1 || entity.getWoodType().ordinal() == matchedPattern - 2;
return matchedPattern <= 1 || entity.getBoatType().ordinal() == matchedPattern - 2;
}

@Override
Expand All @@ -98,35 +87,32 @@ protected int hashCode_i() {

@Override
protected boolean equals_i(EntityData<?> obj) {
if (obj instanceof BoatData)
return matchedPattern == ((BoatData)obj).matchedPattern;
if (obj instanceof BoatData boatData)
return matchedPattern == boatData.matchedPattern;
return false;
}

@Override
public boolean isSupertypeOf(EntityData<?> e) {
if (e instanceof BoatData)
return matchedPattern <= 1 || matchedPattern == ((BoatData)e).matchedPattern;
public boolean isSupertypeOf(EntityData<?> entity) {
if (entity instanceof BoatData boatData)
return matchedPattern <= 1 || matchedPattern == boatData.matchedPattern;
return false;
}

public boolean isOfItemType(ItemType i){
public boolean isOfItemType(ItemType itemType){
int ordinal = -1;

Material type = i.getMaterial();
if (type == Material.OAK_BOAT)
Material material = itemType.getMaterial();
if (material == Material.OAK_BOAT) {
ordinal = 0;
else if (type == Material.SPRUCE_BOAT)
ordinal = TreeSpecies.REDWOOD.ordinal();
else if (type == Material.BIRCH_BOAT)
ordinal = TreeSpecies.BIRCH.ordinal();
else if (type == Material.JUNGLE_BOAT)
ordinal = TreeSpecies.JUNGLE.ordinal();
else if (type == Material.ACACIA_BOAT)
ordinal = TreeSpecies.ACACIA.ordinal();
else if (type == Material.DARK_OAK_BOAT)
ordinal = TreeSpecies.DARK_OAK.ordinal();
} else {
for (Boat.Type boat : types) {
if (material.name().contains(boat.toString())) {
ordinal = boat.ordinal();
break;
}
}
}
return hashCode_i() == ordinal + 2 || (matchedPattern + ordinal == 0) || ordinal == 0;

}
}
Loading

0 comments on commit 1993ab6

Please sign in to comment.