Skip to content

Commit

Permalink
Support for custom model data
Browse files Browse the repository at this point in the history
  • Loading branch information
JVerbruggen committed Jul 1, 2024
1 parent 145f309 commit 0cfd92a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/coaster_yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ There's a lot more types of tracks available, including transfers, launches, and
- - **material**: Icon material
- - **damage**: Damage property
- - **unbreakable**: Unbreakable property
- - **customModelData**: Custom Model Data property
- **warpLocation**: Warp location in [x,y,z,yaw,pitch]
- **warpEnabled**: Whether players should be able to warp to the ride
- **gravityConstant**: Constant that affects gravity-based sections in your coaster
Expand Down
1 change: 1 addition & 0 deletions docs/flatride_yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ plugins/
- - **material**: Icon material
- - **damage**: Damage property
- - **unbreakable**: Unbreakable property
- - **customModelData**: Custom Model Data property
- **warpLocation**: Warp location in [x,y,z,yaw,pitch]
- **warpEnabled**: Whether players should be able to warp to the ride
- **rideOverviewMapId**: Map ID (that is already present in world) that should be used for the ride overview visualization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.jverbruggen.jrides.config.coaster.objects.item;

import com.jverbruggen.jrides.config.coaster.objects.BaseConfig;
import com.jverbruggen.jrides.items.ItemStackFactory;
import com.jverbruggen.jrides.models.entity.TrainModelItem;
import com.jverbruggen.jrides.models.entity.VirtualEntity;
Expand All @@ -29,21 +30,24 @@

import javax.annotation.Nullable;

public class ItemStackConfig implements ItemConfig {
public class ItemStackConfig extends BaseConfig implements ItemConfig {
private final Material material;
private final int damage;
private final boolean unbreakable;
private final int customModelData;

public ItemStackConfig(Material material, int damage, boolean unbreakable) {
public ItemStackConfig(Material material, int damage, boolean unbreakable, int customModelData) {
this.material = material;
this.damage = damage;
this.unbreakable = unbreakable;
this.customModelData = customModelData;
}

public ItemStackConfig() {
this.material = Material.STONE;
this.damage = 0;
this.unbreakable = false;
this.customModelData = -1;
}

public Material getMaterial() {
Expand All @@ -58,6 +62,10 @@ public boolean isUnbreakable() {
return unbreakable;
}

public int getCustomModelData() {
return customModelData;
}

public ItemStack createItemStack(){
return ItemStackFactory.getCoasterStackFromConfig(this);
}
Expand All @@ -70,9 +78,10 @@ public VirtualEntity spawnEntity(ViewportManager viewportManager, Vector3 spawnP
public static ItemStackConfig fromConfigurationSection(@Nullable ConfigurationSection configurationSection) {
if(configurationSection == null) return new ItemStackConfig();

Material material = Material.valueOf(configurationSection.getString("material"));
int damage = configurationSection.getInt("damage", 0);
boolean unbreakable = configurationSection.getBoolean("unbreakable", false);
return new ItemStackConfig(material, damage, unbreakable);
Material material = Material.valueOf(getString(configurationSection, "material", "STONE"));
int damage = getInt(configurationSection, "damage", 0);
boolean unbreakable = getBoolean(configurationSection, "unbreakable", false);
int customModelData = getInt(configurationSection, "customModelData", -1);
return new ItemStackConfig(material, damage, unbreakable, customModelData);
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/jverbruggen/jrides/items/ItemStackBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/************************************************************************************************************
* GPLv3 License *
* *
* Copyright (c) 2024-2024 JVerbruggen *
* https://github.com/JVerbruggen/jrides *
* *
* This software is protected under the GPLv3 license, *
* that can be found in the project's LICENSE file. *
* *
* In short, permission is hereby granted that anyone can copy, modify and distribute this software. *
* You have to include the license and copyright notice with each and every distribution. You can use *
* this software privately or commercially. Modifications to the code have to be indicated, and *
* distributions of this code must be distributed with the same license, GPLv3. The software is provided *
* without warranty. The software author or license can not be held liable for any damages *
* inflicted by the software. *
************************************************************************************************************/

package com.jverbruggen.jrides.items;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;

public class ItemStackBuilder {
private final ItemStack itemStack;
private final ItemMeta itemMeta;

public ItemStackBuilder(Material material) {
this.itemStack = new ItemStack(material, 1);
this.itemMeta = itemStack.getItemMeta();
}

public ItemStackBuilder setDamage(int damage){
((Damageable)this.itemMeta).setDamage(damage);
return this;
}

public ItemStackBuilder setUnbreakable(boolean unbreakable){
this.itemMeta.setUnbreakable(unbreakable);
return this;
}

public ItemStackBuilder setDisplayName(String displayName){
this.itemMeta.setDisplayName(displayName);
return this;
}

public ItemStackBuilder setCustomModelData(int customModelData){
this.itemMeta.setCustomModelData(customModelData);
return this;
}

public ItemStackBuilder setLore(List<String> lore){
if(lore != null)
this.itemMeta.setLore(lore);

return this;
}

public ItemStack build(){
this.itemStack.setItemMeta(itemMeta);
return this.itemStack;
}

}
28 changes: 15 additions & 13 deletions src/main/java/com/jverbruggen/jrides/items/ItemStackFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,33 @@
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

import java.util.List;

import javax.annotation.Nullable;

public class ItemStackFactory {
public static ItemStack getCoasterStack(Material material, Integer damageValue, boolean unbreakable) {
return getStack(material, damageValue, unbreakable, ChatColor.GOLD + "jrides model", null);
return new ItemStackBuilder(material)
.setDisplayName(ChatColor.GOLD + "jrides model")
.setDamage(damageValue)
.setUnbreakable(unbreakable)
.build();
}

public static ItemStack getStack(Material material, Integer damageValue, boolean unbreakable, String displayName, @Nullable List<String> lore) {
ItemStack stack = new ItemStack(material, 1);
ItemMeta meta = stack.getItemMeta();
((Damageable)meta).setDamage(damageValue);
meta.setDisplayName(displayName);
meta.setUnbreakable(unbreakable);
if(lore != null) meta.setLore(lore);
stack.setItemMeta(meta);
return stack;
public static ItemStack getCoasterStack(Material material, int customModelData){
return new ItemStackBuilder(material)
.setDisplayName(ChatColor.GOLD + "jrides model")
.setCustomModelData(customModelData)
.build();
}

public static ItemStack getCoasterStackFromConfig(ItemStackConfig itemStackConfig){
int customModelData = itemStackConfig.getCustomModelData();
if(customModelData != -1){
return getCoasterStack(itemStackConfig.getMaterial(), customModelData);
}

return getCoasterStack(itemStackConfig.getMaterial(), itemStackConfig.getDamage(), itemStackConfig.isUnbreakable());
}

Expand Down

0 comments on commit 0cfd92a

Please sign in to comment.