Skip to content

Commit 20d0206

Browse files
committed
輸入組件現在可以接受null數值
1 parent 2f8cdb0 commit 20d0206

File tree

9 files changed

+44
-38
lines changed

9 files changed

+44
-38
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
env:
2-
version: 0.1.3 # 你的版本名稱
2+
version: 0.1.3.1 # 你的版本名稱
33
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
plugin_name: ELDependenci-MVC-plugin
55

ELDependenci-MVC-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>ELDependenci-MVC-plugin</artifactId>
13-
<version>${project.parent.version}</version>
13+
<version>${project.parent.version}.1</version>
1414

1515
<dependencies>
1616
<dependency>

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/ELDGUI.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
import java.lang.annotation.Annotation;
2828
import java.lang.reflect.Method;
2929
import java.lang.reflect.ParameterizedType;
30-
import java.util.Arrays;
31-
import java.util.List;
32-
import java.util.Map;
33-
import java.util.Optional;
30+
import java.util.*;
3431
import java.util.concurrent.ConcurrentHashMap;
3532
import java.util.function.Consumer;
3633
import java.util.function.Function;
@@ -226,48 +223,42 @@ private void initMethodParseManager(MethodParseManager parser) {
226223
parser.registerParser((t, annos) -> Arrays.stream(annos).anyMatch(a -> a.annotationType() == ModelAttribute.class),
227224
(annotations, type, event) -> {
228225
ModelAttribute modelAttribute = (ModelAttribute) Arrays.stream(annotations).filter(a -> a.annotationType() == ModelAttribute.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find @ModelAttribute"));
229-
var context = this.currentView.getEldgContext();
230226
if (type instanceof ParameterizedType)
231227
throw new IllegalStateException("model attribute cannot be generic type");
232228
var model = ((Class<?>) type);
233-
234-
Map<String, Object> fieldMap = context.getItems(modelAttribute.value())
235-
.stream()
236-
.filter(item -> context.getAttribute(item, AttributeController.FIELD_TAG) != null)
237-
.collect(Collectors
238-
.toMap(
239-
item -> context.getAttribute(item, AttributeController.FIELD_TAG),
240-
item -> Optional.ofNullable(context.getAttribute(item, AttributeController.VALUE_TAG)).orElseThrow(() -> new IllegalStateException("The value tag of " + item.toString() + " is null."))
241-
)
242-
);
229+
var fieldMap = getFieldMap(modelAttribute.value());
243230
Map<String, Object> toConvert = PersistDataUtils.toNestedMap(fieldMap);
244231
LOGGER.debug("using " + toConvert + " to create instance of " + model);
245232
return PersistDataUtils.mapToObject(toConvert, model);
246233
});
247234
parser.registerParser((t, annos) -> Arrays.stream(annos).anyMatch(a -> a.annotationType() == MapAttribute.class),
248235
(annotations, type, event) -> {
249236
MapAttribute attribute = (MapAttribute) Arrays.stream(annotations).filter(a -> a.annotationType() == MapAttribute.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find MapAttribute annotation"));
250-
var context = this.currentView.getEldgContext();
251237
boolean isMap = false;
252238
if (type instanceof ParameterizedType) {
253239
var parat = (ParameterizedType) type;
254240
isMap = parat.getRawType() == Map.class && parat.getActualTypeArguments()[0] == String.class && parat.getActualTypeArguments()[1] == Object.class;
255241
}
256242

257243
if (!isMap) throw new IllegalStateException("@MapAttribute 必須使用 Map<String, Object> 作為其類型");
258-
Map<String, Object> fieldMap = context.getItems(attribute.value())
259-
.stream()
260-
.filter(item -> context.getAttribute(item, AttributeController.FIELD_TAG) != null)
261-
.collect(Collectors
262-
.toMap(
263-
item -> context.getAttribute(item, AttributeController.FIELD_TAG),
264-
item -> Optional.ofNullable(context.getAttribute(item, AttributeController.VALUE_TAG)).orElseThrow(() -> new IllegalStateException("The value tag of " + item.toString() + " is null."))
265-
)
266-
);
244+
Map<String, Object> fieldMap = getFieldMap(attribute.value());
267245
return PersistDataUtils.toNestedMap(fieldMap);
268246
});
269247
}
270248

249+
private Map<String, Object> getFieldMap(char pattern){
250+
if (this.currentView == null) throw new IllegalStateException("currentView is null");
251+
var context = this.currentView.getEldgContext();
252+
Map<String, Object> fieldMap = new HashMap<>();
253+
for (ItemStack item : context.getItems(pattern)) {
254+
String field = context.getAttribute(item, AttributeController.FIELD_TAG);
255+
if (field == null) continue;
256+
Object value = context.getAttribute(item, AttributeController.VALUE_TAG);
257+
fieldMap.put(field, value);
258+
}
259+
return fieldMap;
260+
}
261+
271262
private ItemStack getItemByEvent(InventoryEvent e) {
272263
return Optional.ofNullable(e).map(ee -> itemGetterMap.get(ee.getEventName())).map(f -> f.apply(e)).orElseThrow(() -> new IllegalStateException("no item return by the event or the event is null"));
273264
}

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/ELDGView.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,15 @@ public <C> C getAttributePrimitive(Class<C> type, ItemStack itemStack, String ke
333333

334334
public Map<String, Object> getAsMap(ItemStack item) {
335335
String id = getAttributePrimitive(String.class, item, "id");
336-
return Optional.ofNullable(attributeMap.get(id)).map(ImmutableMap::copyOf).orElseGet(ImmutableMap::of);
336+
return Optional.ofNullable(attributeMap.get(id)).map(HashMap::new).orElseGet(HashMap::new);
337337
}
338338

339339
@Override
340-
public <C> C getAttribute(ItemStack item, String key) {
340+
public synchronized <C> C getAttribute(ItemStack item, String key) {
341341
// instead of using persist data type, use map
342342
//return getObjectAttribute(item, key);
343343
String id = getIdFromItem(item);
344-
attributeMap.putIfAbsent(id, new ConcurrentHashMap<>());
344+
attributeMap.putIfAbsent(id, new HashMap<>());
345345
LOGGER.debug("item (" + item.getType() + ") is now: " + getAsMap(item).toString());
346346
return (C) attributeMap.get(id).get(key);
347347
}
@@ -393,11 +393,11 @@ public <C> void setAttributePrimitive(Class<C> type, ItemStack itemStack, String
393393
}
394394

395395
@Override
396-
public void setAttribute(ItemStack itemStack, String key, Object value) {
396+
public synchronized void setAttribute(ItemStack itemStack, String key, Object value) {
397397
// instead of using persist data type, use map
398398
//this.setObjectAttribute(itemStack, key, value);
399399
String id = getIdFromItem(itemStack);
400-
this.attributeMap.putIfAbsent(id, new ConcurrentHashMap<>());
400+
this.attributeMap.putIfAbsent(id, new HashMap<>());
401401
this.attributeMap.get(id).put(key, value);
402402

403403
LOGGER.debug("item (" + itemStack.getType() + ") is now: " + getAsMap(itemStack).toString());
@@ -410,7 +410,7 @@ public <C> void setAttributePrimitive(Class<C> type, char pattern, String key, O
410410

411411

412412
@Override
413-
public void setAttribute(char pattern, String key, Object value) {
413+
public synchronized void setAttribute(char pattern, String key, Object value) {
414414
getItems(pattern).forEach(item -> setAttribute(item, key, value));
415415
}
416416

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/PersistDataUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15+
import javax.annotation.Nonnull;
1516
import java.io.IOException;
1617
import java.lang.reflect.Field;
1718
import java.lang.reflect.InvocationTargetException;
@@ -146,6 +147,9 @@ public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
146147
Map<String, Object> m = (Map<String, Object>) value;
147148
value = mapToObject(m, field.getType());
148149
}
150+
if (value == null && field.isAnnotationPresent(Nonnull.class)){
151+
throw new IllegalStateException("property assigned @Nonnull but setting null value.");
152+
}
149153
try {
150154
field.set(obj, value);
151155
}catch (IllegalAccessException e) {

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/demo/error/StaticErrorView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void renderView(Exception ex, UIContext context) {
2626
.components(
2727
button.icon(Material.BARRIER)
2828
.title("&cError: " + ex.getClass().getSimpleName())
29-
.lore("&c".concat(ex.getMessage()))
29+
.lore("&c" + ex.getMessage())
3030
.create()
3131
);
3232
}

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/demo/test/TestController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public void beforeCreate(Player player){
2525

2626

2727
@ClickMapping(view = TestView.class, pattern = 'A')
28-
public void onClick(@ModelAttribute('Z') TestModel test, Player player, @MapAttribute('Z') Map<String, Object> map){
28+
public BukkitView<?, ?> onClick(@ModelAttribute('Z') TestModel test, Player player, @MapAttribute('Z') Map<String, Object> map){
2929
player.sendMessage(test.toString());
3030
player.sendMessage(map.toString());
31+
return null;
3132
}
3233

3334

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/demo/test/TestModel.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ public class TestModel {
1313

1414
public LocalTime testTime;
1515

16+
// test null
17+
public String txt;
18+
1619
@Override
1720
public String toString() {
1821
return "TestModel{" +
19-
"testColor=" + testColor.toString() +
20-
", testDate=" + testDate.toString() +
21-
", testTime=" + testTime.toString() +
22+
"testColor=" + testColor +
23+
", testDate=" + testDate +
24+
", testTime=" + testTime +
25+
", txt='" + txt + '\'' +
2226
'}';
2327
}
2428
}

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/demo/test/TestView.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ericlam.mc.eldgui.demo.test;
22

3+
import com.ericlam.mc.eldgui.component.AttributeController;
34
import com.ericlam.mc.eldgui.component.factory.ButtonFactory;
45
import com.ericlam.mc.eldgui.component.factory.DateSelectorFactory;
56
import com.ericlam.mc.eldgui.component.factory.RGBSelectorFactory;
@@ -47,6 +48,11 @@ public void renderView(Void model, UIContext context) {
4748
.bindInput("testTime", LocalTime.now())
4849
.label("&aTime Select: (shift move unit, click to +/-, middle to input)")
4950
.icon(Material.CLOCK)
51+
.create(),
52+
button.icon(Material.PAPER)
53+
.title("test null string")
54+
.bind(AttributeController.FIELD_TAG, "txt")
55+
.bind(AttributeController.VALUE_TAG, null)
5056
.create()
5157
)
5258
.and()

0 commit comments

Comments
 (0)